Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
Check out Non-printing Characters and \[LetterSpace]. – dionys Mar 29 at 12:57
    
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. – Edmund Mar 29 at 12:59
    
@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? – becko Mar 29 at 13:09
1  
You can use $the$name$of$my$variable – R. M. Mar 29 at 14:07
1  
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. – andre Mar 29 at 16:48

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.

share|improve this answer
2  
"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... – J. M. Mar 29 at 13:22

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:

share|improve this answer
1  
...and to verify that they are indeed valid symbols, use SyntaxQ[]. – J. M. Mar 29 at 13:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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