46

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?

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
user565739
  • 1,119
  • 1
  • 9
  • 14
  • 2
    firstPolynomialDegree ? – Artes Sep 06 '13 at 08:10
  • 4
    Perhaps try using indexes, polynomialDegree[1], polynomialDegree[2] etc. – C. E. Sep 06 '13 at 08: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 06 '13 at 08: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 06 '13 at 15:27
  • @jVincent I think it is a reasonable reading of the "don't ask" section of the FAQ that "what do you use?" questions are too open-ended. However, in this case I do not think the question is open-ended, and I am inclined to reopen it even if the fifth close vote is cast. I don't think there are an endless number of different ways to approach this unless you count every pet "space" character. The basic options are those that I already outlined, unless I've forgotten a major one. – Mr.Wizard Sep 06 '13 at 15:56
  • @Mr.Wizard You forgot arguably "the" pet space character, namely LetterSpace. Who's sole purpose in life I would suspect is to "solve" this exact desire for underscore separated compounds. – jVincent Sep 06 '13 at 16:06
  • @jVincent I don't agree with your assessment. As you note yourself in your own answer using LetterSpace would be a confusing practice. More importantly it is already covered in my answer, just not mentioned explicitly by name. The "I like character a, I like character b" answers are exactly what I don't think we want to encourage with a question like this. (Sorry, but I have to be honest.) Instead if you have another concept besides non-alphanumeric-special-characters in Symbol names or indexed variables as mentioned by Anon, I'd love to hear about it. – Mr.Wizard Sep 06 '13 at 16:13
  • @Mr.Wizard I was not commenting on preference, I already noted, I don't like LetterSpace, I was answering your own very specific inquiry "unless I've forgotten a major one". The fact that LetterSpace is named "letter space" should be some indication that it has a preference status as letters that act as substitutes for space, which does make it worth special mention prior to more exotic replacements like ellipsis. Not due to preference, but due to it's purpose. – jVincent Sep 06 '13 at 16:16
  • @jVincent Okay, I'll accept that argument. You'll get my vote but it's the last answer with an alternative character that will. The OP got one for answering his own quesiton; you get one for what appears to be the canonical replacement for raw underscore. – Mr.Wizard Sep 06 '13 at 16:19
  • @jVincent But, imo it is opinion based. You use the naming convention because you find it convenient not because it is absolutely better. I like the question, but rules are rules or are we creating exceptions? – Kuba Sep 06 '13 at 16:22
  • @Mr.Wizard I think this question fits well closure reason description but since I like I do not mind reopening. – Kuba Sep 06 '13 at 16:25
  • @Kuba jVincent has a reasonable argument that he has the objective canonical answer as the documentation says: "Used to separate words visually, but treated as a letter for purposes of Mathematica input." I argue that my own answer is not subjective but instead an overview of the practical methods available; I will extend it if someone can show me another. – Mr.Wizard Sep 06 '13 at 16:28
  • 3
    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 http://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 06 '13 at 18:04

3 Answers3

27

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

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • Thanks. Just one thing, the dash doesn't work for function parameter, like first-polynomial-degree_. – user565739 Sep 06 '13 at 08: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 06 '13 at 08:42
  • @Mr.Wizard I does not work on V9. :/ In all the code only c_ is colored as a pattern, but not c in the body. – Kuba Sep 06 '13 at 08:45
  • I am using V8, the same problem as Kuba mentioned – user565739 Sep 06 '13 at 08:51
  • @Kuba Is there another character from the palette that does work in this application and which also seems like a good space proxy? – Mr.Wizard Sep 06 '13 at 09:11
  • 3
    @Mr.Wizard \[CenterEllipsis], \[TripleDot] work and look similar. Dash, LongDash, Hyphen do not work. – Kuba Sep 06 '13 at 09:19
  • It documentation of V7 saying that \[Dash] is Letter-like form? – Kuba Sep 06 '13 at 10:26
  • 2
    FYI in version 9 a\[Dash]b is interpreted as Times[a,b,\[Dash]], no idea why this has changed. – Simon Woods Sep 06 '13 at 10:27
  • \[Prime] can be used, is relatively easy to input, Esc ' Esc, and looks a little like a slanted dash in the Front End. – Michael E2 Sep 06 '13 at 11:07
  • @MichaelE2 For some reason I don't like that one. It doesn't make a very good space IMHO, and it also has a natural meaning, which isn't this. One can always add an input alias for something like \[TripleDot] if it is required. – Mr.Wizard Sep 06 '13 at 11:12
  • 2
    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 06 '13 at 11:26
  • @Michael Points well taken. You know my predilection regarding code length therefore I generally avoid this problem entirely. I think dynamicPartition is about the longest symbol name I've ever used. :-) – Mr.Wizard Sep 06 '13 at 11:30
22

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

jVincent
  • 14,766
  • 1
  • 42
  • 74
4

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[{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]
xiaohuamao
  • 4,728
  • 18
  • 36
user565739
  • 1,119
  • 1
  • 9
  • 14
  • 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 06 '13 at 12:11
  • Please try to paste the code; if you are seeing things like \[charactername] that is normal. – Mr.Wizard Sep 06 '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 06 '13 at 12:24
  • For the part with FromCharacterCode I receive only empty squares. – Kuba Sep 06 '13 at 12:25
  • @Kuba, I don't know. I am using Windows 7 x64. – user565739 Sep 06 '13 at 12:28
  • 4
    Self-answering is completely acceptable: http://meta.stackexchange.com/questions/17463/can-i-answer-my-own-questions-even-if-i-knew-the-answer-before-asking – Yves Klett Sep 06 '13 at 12:59
  • I can confirm that your Symbol name works in version 7 under windows, at least in the simple case here. +1 – Mr.Wizard Sep 06 '13 at 13:52
  • 3
    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 06 '13 at 15:34
  • Perhaps some intermediate solution such as Esc+u(+Esc or u[ or u{ is both nice to the eyes and not confusing with the standard syntax. But they are still not too nice if you then want to copy your code to a .m file I guess – Rojo Sep 06 '13 at 21:43