Guidelines on giving readable names to variables [closed]

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?

• firstPolynomialDegree ? Sep 6 '13 at 8:10
• Perhaps try using indexes, polynomialDegree[1], polynomialDegree[2] etc. Sep 6 '13 at 8:13
• 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. Sep 6 '13 at 15:27
• 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

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:

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

• Thanks. Just one thing, the dash doesn't work for function parameter, like first-polynomial-degree_. 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 Sep 6 '13 at 8:42
• @Mr.Wizard \[CenterEllipsis], \[TripleDot] work and look similar. Dash, LongDash, Hyphen do not work.
– Kuba
Sep 6 '13 at 9:19
• FYI in version 9 a\[Dash]b is interpreted as Times[a,b,\[Dash]], no idea why this has changed. Sep 6 '13 at 10:27
• 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. 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:

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

Sorry to answer my own question. But I think it's worth keeping 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)

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]

• 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. 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. Sep 6 '13 at 12:24
• Self-answering is completely acceptable: meta.stackexchange.com/questions/17463/… Sep 6 '13 at 12:59
• 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. Sep 6 '13 at 15:34