Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I know that one cannot use underscore to a variable name, like degree_of_The_First_Polynomial. But for a long variable name, it is difficult to read something like degreeOfTheFirstPolynomial.

What do you do when you need a clear, self-explained, easy-to-read variable name?

share|improve this question

closed as primarily opinion-based by Kuba, Yves Klett, Nasser, m_goldberg, rm -rf Sep 9 '13 at 22:23

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
firstPolynomialDegree ? –  Artes Sep 6 '13 at 8:10
3  
Perhaps try using indexes, polynomialDegree[1], polynomialDegree[2] etc. –  Pickett Sep 6 '13 at 8:13
1  
Maybe this is not a bad question but unfortunatelly it fits well to: "primarily opinion-based" which is an argument for closing. I would use PolyDeg1. For a "space" i usualy use $ or $$ but do not use it on the beginning of the name. –  Kuba Sep 6 '13 at 8:16
    
@Kuba While it's true that the question would be primarily opinion based if it asked what the best method was, the question as it stands is asking what methods people use which is not opinion based, even if people do use different methods. –  jVincent Sep 6 '13 at 15:27
1  
For readable variable names, my own preference is to follow what Mathematica itself does for practically all function names that are composed of several words: use en.wikipedia.org/wiki/Camelcase, except for the first letter of the variable which should be lower case to distinguish it from built-in names (which are always capitalized). –  Jens Sep 6 '13 at 18:04

3 Answers 3

You can use String "keys" for indexed variables, as I did for A combination of Set::setraw and Set::shape errors. The strings can have spaces or any other characters you want to use:

var["Degree of the First Polynomial"] = (* stuff *);

You also have a wide range of characters, many of which can be used in Symbol names.
Go to menu Palettes > Special Characters.

You can use these to create short but unique Symbols:

enter image description here

Or you can use e.g. \[TripleDot] for long ones:

enter image description here

share|improve this answer
    
Thanks. Just one thing, the dash doesn't work for function parameter, like first-polynomial-degree_. –  user565739 Sep 6 '13 at 8:38
    
@user565739 It does for me in version 7; are you using \[Dash] entered with Esc? Try this: f[a\[Dash]b\[Dash]c_] := a\[Dash]b\[Dash]c^2 –  Mr.Wizard Sep 6 '13 at 8:42
2  
@Mr.Wizard \[CenterEllipsis], \[TripleDot] work and look similar. Dash, LongDash, Hyphen do not work. –  Kuba Sep 6 '13 at 9:19
2  
FYI in version 9 a\[Dash]b is interpreted as Times[a,b,\[Dash]], no idea why this has changed. –  Simon Woods Sep 6 '13 at 10:27
1  
It's a matter of taste, I suppose, and getting used to things. \[Prime] does not look like a prime, when used in a variable name at least, so in reading it, I associate no natural meaning to it. Just to illustrate the difference in taste/perspective, \[TripleDot] looks like an ellipsis, which has a natural meaning, too, and I see the words as separate variables. I could get used to it, and then it wouldn't seem so strange. Personally I've gotten used to the capitalization trick the OP wants to avoid. –  Michael E2 Sep 6 '13 at 11:26

I mentioned this in a comment, but I believe this might really be the correct answer to your specific inquiry about underscore. You can escape underscore (Esc+_+Esc or Ecs+ls+Esc), which will give you a \[LetterSpace], which looks like underscore but is slightly lighter. This is just treated like a regular old letter and you can therefore use it in variable names like any other character.

So you have

f[my\[LetterSpace]list_List]:=my\[LetterSpace]list
f[2]
f[{2,3}]

Which displays like:

enter image description here

Personally I would say that this is bad form as you are deliberately trying to look like something that has syntactic value without actually being it.

Here's an introduction to other letter-like forms which could also be used in variable names depending on preferences. Tutorial Letters And Letter Like Forms

share|improve this answer

Sorry to answer my own question. But I think it worth to keep this.

I checked similar Unicode symbols of underscore - and hyphen -, and I got the following usable alternatives: (I can't paste the code directly, some problem)

enter image description here

Here is the code: (Problems with unicdoe)

FromCharacterCode[{8210}]
FromCharacterCode[{717, 0, 817, 0, 818, 0, 8215, 0, 65343}]
This‒exampleˍshows̱it̲works‗very_well = 3
f[This‒exampleˍis̱for̲function‗parameter_name_] := Print["Yes!"]
f[any]
share|improve this answer
    
Hyphen does not work in V9. What is underscore? Also I can't reproduce your output. Hyphen is 8208 for WinXP, but I;m not sure if that's the reason of the difference. –  Kuba Sep 6 '13 at 12:11
    
Please try to paste the code; if you are seeing things like \[charactername] that is normal. –  Mr.Wizard Sep 6 '13 at 12:24
    
@Kuba, Hyphen (8208) doesn't work. What I used above is 8210, something similar to Hyphen. undersocre is just _ and the above doesn't use it, just something similar to it. –  user565739 Sep 6 '13 at 12:24
3  
Self-answering is completely acceptable: meta.stackexchange.com/questions/17463/… –  Yves Klett Sep 6 '13 at 12:59
1  
I'll take a guess and assume that you think this_variable looks "better" than thisVariable, due to experiance with other languages. I'll just emphasise the in Mathematica this_List has a completely different meaning than it does in such languages. It's a pattern that matches expressions with head List, and names it this. I would say that deliberately trying to name variables so that they'll look like this should be considered harmful. That being said, you can escape underscore and it'll work like you want, so Esc+_+Esc. –  jVincent Sep 6 '13 at 15:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.