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?

  • 2
    $\begingroup$ firstPolynomialDegree ? $\endgroup$ – Artes Sep 6 '13 at 8:10
  • 4
    $\begingroup$ Perhaps try using indexes, polynomialDegree[1], polynomialDegree[2] etc. $\endgroup$ – C. E. Sep 6 '13 at 8:13
  • 1
    $\begingroup$ 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. $\endgroup$ – Kuba Sep 6 '13 at 8:16
  • $\begingroup$ @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. $\endgroup$ – jVincent Sep 6 '13 at 15:27
  • 3
    $\begingroup$ 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). $\endgroup$ – 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:

enter image description here

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

enter image description here

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


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


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)

enter image description here

Here is the code: (Problems with unicdoe)

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!"]
  • $\begingroup$ 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. $\endgroup$ – Kuba Sep 6 '13 at 12:11
  • $\begingroup$ Please try to paste the code; if you are seeing things like \[charactername] that is normal. $\endgroup$ – Mr.Wizard Sep 6 '13 at 12:24
  • $\begingroup$ @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. $\endgroup$ – user565739 Sep 6 '13 at 12:24
  • 3
    $\begingroup$ Self-answering is completely acceptable: meta.stackexchange.com/questions/17463/… $\endgroup$ – Yves Klett Sep 6 '13 at 12:59
  • 2
    $\begingroup$ 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. $\endgroup$ – jVincent Sep 6 '13 at 15:34

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