Search code examples
naming-conventionslanguage-design

Why don't many languages accept names starting from a digit?


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?


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