21
$\begingroup$

In C, _ comes in handy when naming variables and functions, as a replacement for space. For example:

int the_name_of_my_variable;

I find this more readable than CamelCase:

int theNameOfMyVariable;

That's just my taste.

In Mathematica we cannot use _ to replace spaces in variable names, because _ means Blank pattern. Is there an alternative we can use? Another character that is not an operator and can be used as a replacement for space?

$\endgroup$
5
  • $\begingroup$ Check out Non-printing Characters and \[LetterSpace]. $\endgroup$
    – dionys
    Mar 29, 2016 at 12:57
  • $\begingroup$ You may use any Letter and Letter Like Form as long as it is not a defined symbol. Smiley face, ellipse, white bishop, black king, wolfie; anything. However, I think it will make your code hard to read for others. $\endgroup$
    – Edmund
    Mar 29, 2016 at 12:59
  • $\begingroup$ @dionys \[LetterSpace] is close to what I want. But I want something I can see. Is there a form of underscore that is not a symbol? $\endgroup$
    – a06e
    Mar 29, 2016 at 13:09
  • 2
    $\begingroup$ You can use $the$name$of$my$variable $\endgroup$
    – rm -rf
    Mar 29, 2016 at 14:07
  • 4
    $\begingroup$ In the past Wolfram Research had the same problem when they implemented NETLink. Their solution was to replace _ by U (see here, and look for "underscore"). It's a clue that there is probably no better way to do that. $\endgroup$
    – andre314
    Mar 29, 2016 at 16:48

4 Answers 4

34
$\begingroup$

This has been discussed on comp.soft-sys.math.mathematica. The gist is that there are lots of Unicode characters you could use, e.g. \[LetterSpace] or \[UnderBracket] (you could consult https://reference.wolfram.com/language/tutorial/LettersAndLetterLikeForms.html for a long list), but I'd strongly urge you not to do that.

Once you copy the code out of Mathematica, these will be expanded to the above long forms which will make your code very unreadable. I think there are ways to "copy as Unicode", but a lot of these are in Unicode's private use pages, which means they will almost certainly not render properly anywhere outside of Mathematica.

Plus, camelCase is such a widespread convention in Mathematica, that it's probably not worth the trouble working around that just to match your programming style in another language. Mathematica's syntax works differently from C's, and I don't think there's a lot of benefit in trying to make Mathematica look like C. You'll probably save yourself a lot of trouble by simply embracing Mathematica's own conventions.

$\endgroup$
3
  • 10
    $\begingroup$ "it's probably not worth the trouble working around that just to match your programming style in another language." - I upvoted specifically because of this. When in Rome... $\endgroup$ Mar 29, 2016 at 13:22
  • $\begingroup$ Keep in mind that visual parsing of text by the human brain is trained at an early age, and not easily updated. I have been using CamelCase for 2 decades and I still have to slow down to read it, whereas words_separated_with_underscore_I_can_read_at_full_speed. $\endgroup$ Jan 1, 2021 at 7:54
  • 1
    $\begingroup$ @SheblaTsama I agree that snake_case is a lot more readable, and if I could choose I'd always use it, but I've found that going against language conventions is generally not worth it. $\endgroup$ Jan 5, 2021 at 16:03
13
$\begingroup$

The issue is that Mathematica interprets underscore as Blank and interprets my_variable as a Pattern, when what we'd like is a legitimate symbol name.

    Head[Unevaluated[my_variable]]
    (* Pattern *)

There are a few Unicode alternatives for underscore:

  • combining low-line

  • combining macron below

  • modifier low-letter macron

  • figure dash

  • fullwidth low-line

     varNames = StringReplace["my_variable", "_" -> FromCharacterCode[{#}]] & /@ {717, 817, 818, 8210,65343}
    
     (* {"myˍvariable", "my̱variable", "my̲variable", "my‒variable", "my_variable"} *)
    
     Head[Unevaluated[#]] & /@ (ToExpression /@ varNames)
     (* {Symbol, Symbol, Symbol, Symbol, Symbol} *)
    
     varNameQ[s_String] := Check[Symbol[s]; True, False] // Quiet
    
     varNameQ /@ varNames
     (* {True, True, True, True, True} *)
    
$\endgroup$
5
  • 1
    $\begingroup$ ...and to verify that they are indeed valid symbols, use SyntaxQ[]. $\endgroup$ Mar 29, 2016 at 13:58
  • $\begingroup$ @J.M. that won't verify that these symbols are valid symbols for variables. E.g. SyntaxQ["1hel_lo5"] also returns True. $\endgroup$
    – Ruslan
    Sep 29, 2018 at 19:02
  • $\begingroup$ @Ruslan, indeed, so one needs to do another test like StringMatchQ["1hel_lo5", WordCharacter ..]. $\endgroup$ Sep 29, 2018 at 19:09
  • $\begingroup$ @J.M. I think Symbol["1hel_lo5"] would be a more direct check (if it returns unevaluated and emits Symbol::symname, the symbol is bad, otherwise OK). $\endgroup$
    – Ruslan
    Sep 29, 2018 at 19:16
  • $\begingroup$ @Ruslan, you'd need to wrap Quiet[] around it if you don't want messages being thrown just for testing, however. $\endgroup$ Sep 29, 2018 at 19:17
8
$\begingroup$

I use $ as a separator. It is a valid symbol character.

In my application I have a set of variables that need to be identified by coordinates, like for example a17_4_25. I can't just drop the _ since a17425 would be ambiguous, so I use a17\$4\$25.

It's not as pleasing to the eye as '_', but it gets the job done and it is plain ascii.

Obviously CamelCase would not work for me. I would love to find another ascii character that provides more visual separation than \$ but as far as I know there is none.

EDIT: I just did an exhaustive trial of ascii characters and there is no other choice: \$ is the only option!

$\endgroup$
1
  • $\begingroup$ Why not 17x4x25? We used x for multiplication in early school years way back when and the sky did not fall. $\endgroup$
    – Nicholas G
    Aug 15, 2022 at 12:31
2
$\begingroup$

OK, I decide to add this as an answer. \[Ellipsis] () is also a possible choice to replace underscore. It owns at least 3 advantages:

  1. It can be converted by Mathematica Editor-buttons for StackExchange so it looks nice in this site.

  2. Its input alias in Mathematica is Esc...Esc, which is relatively easy to type.

  3. It's a commonly used character for ellipisis. Even if Mathematica isn't available, it can be inputted in a relatively easy way (at least for Chinese IME).

$\endgroup$
3
  • $\begingroup$ Agreed. I started using it after I saw your answer. I also use esc+bv+esc to indicate space between words and symbol1 esc+bu+esc symbol2 when I want to indicate a modification of type symbol2 to symbol1 or some sort of relation like the "." in Python. The issue with esc+bv+esc is that it does not look the same way on stack exchange. $\endgroup$ Nov 23, 2022 at 6:30
  • $\begingroup$ I meant your answer on FFT fluid mechanics. Also I get a bit worried that people might confuse it with .. or some sort of pattern matching. $\endgroup$ Nov 23, 2022 at 6:36
  • 1
    $\begingroup$ @userrandrand Yeah, that's the reason I decide to add this answer. From now on I can cite it when someone gets confused :) . $\endgroup$
    – xzczd
    Nov 23, 2022 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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