Mathematica: Webel: You CAN/MAY use $ in variable names and function names - just not as the first character before a Capital - and it's extremely useful. You won't get sent to Azkaban prison if do you use a $ character!

Webel IT Australia promotes the amazing Mathematica tool and the powerful Wolfram Language and offers professional Mathematica services for computational computing and data analysis. Our Mathematica tips, issue tracking, and wishlist is offered here most constructively to help improve the tool and language and support the Mathematica user community.
DISCLAIMER: Wolfram Research does not officially endorse analysis by Webel IT Australia.
Icon class
icon_class
far fa-sticky-note
icon_class_computed
far fa-sticky-note
Note kind
Policy level
Keywords
Dr Darren says:
I use the '$' character in Mathematica function names and variable names often, and at the time of writing it has not yet killed me, nor caused any major issues (except one very inconvenient and annoying one with Wolfram Workbench). I even often use it as the first character, just not before a Capital. Take a walk on the wild side, try it.
(BTW: The highly recommended Wolfram Language Plugin for IntelliJ handles '$' characters in "function" names just splendidly.)
Just don't use $ as the first character before a Capital letter, because that is reserved for Mathematica system variables like $MachinePrecision.

The '$' is also used by MMA in some internally generated variables names, such as for local variable names within Modules.


Module[{x}, x]

x$45915
To avoid clashes just avoid using a 5-digit integer directly after a '$'.

There are not many other useful punctuation characters you can use in variable and function names easily in Mathematica. For example, the underscore '_', which is often used in variable names in many coding languages, has a special meaning for pattern matching in MMA. The '.' is of course used mathematically. There '#' is taken for virtual functions. The '@' is used for function chaining and '@@' for Apply. The '/' is used in '/.' (for rules substitutions) and in '/@' (for Map).

The '$' is sometimes used as the first letter of an identifier to indicate a constant

One thing I like to do with the '$' is use it for association keys, so you can then prompt on the $, and even use it for grouping, like this:


$entity$this = "entity$this";
$entity$that = "entity$that";

Once it's setup, you can then prompt on them via the $ and walk the keys like a tree. Nice and DRY!

And you can also use them for option names, with nice prompting, like this:


$opt$doThis = "doThis";
$opt$doThat = "doThat";

Options[fDRY] = {$opt$doThis->True,$opt$doThat->False}

f[v,OptionsPattern[]] := If[OptionValue[$opt$doThis], ..., ...] ...

It can be used as keys for a Mathematica Entity with a form of namespacing. For example:

$e$Example = "e$Example"; (* Entity type *)
$e$Example$Sub$a = "e$Example$Sub$a";
$e$Example$Sub$b = "e$Example$Sub$b";

You can then prompt your way through $e$*$*$* in your Notebook editor (works well) or Workbench IDE (not so well) without having to remember your data structure or keys. No remembering of typing or tedious Strings! For example, to find all entities of that type: EntityList[$e$Example]'

In some cases this strategy can be used as preparation for migration to a more OO approach.

Together with Mathematica's strong expression identifier awareness and prompting, this tip can save you a lot of typing, and can make for more robust code.

One reason for using the '$' in variables names is to support better prompting, such as illustrated here:

Possible issues

Relates to
Related notes
Related notes (backlinks)
Related snippets (extracts)
Visit also
Visit also (backlinks)
External links