Question

I am always bumping into a curious fact while reading any programming language reference:

Variable or constant name cannot start with a digit

Of course, even if names from digit were allowed, it would be a bad practice to use such.

But what are the main reasons really?

  • Is it going to be so hard to parse?

  • Is it deprecated in order to not to obfuscate a code?

Was it helpful?

Solution

This restriction exists in order to simplify the language parsers. The work needed to accept identifiers with leading digits is probably not considered worth the complexity.

Not all languages have that restriction though; consider Racket (a Lisp/Scheme dialect):

pu@pumbair: ~  racket
Welcome to Racket v5.3.6.
-> (define 9times! 9)
-> (* 9times! 2)
18

but then of course Lisp languages are particularly easy to parse.

As for obfuscation, I'm sure that the fact that identifiers can be unicode characters (such as in Racket and Go) can be way more confusing:

-> (define ǝʃqɐıɹɐʌ-ɐ-sı-sıɥ⊥ 144)
-> (sqrt ǝʃqɐıɹɐʌ-ɐ-sı-sıɥ⊥)
12

OTHER TIPS

To make a parsing efficient a parser relies on looking ahead at the next character to determine the possibilities of the next token. When identifiers such as variable names, constant names and words can start with a digit, then the number of possibilities to branch on for the next token go up dramatically. Also depending on the parsing method, it may have to look ahead more characters to determine the token type which leads to greater complexity with the parser.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top