Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing numbers to be the first character of a variable (e.g 1abc) #48

Open
MrCoder opened this issue Sep 7, 2023 · 1 comment
Open

Comments

@MrCoder
Copy link
Collaborator

MrCoder commented Sep 7, 2023

Background

When the DSL was first designed, it was heavily inspired by main stream OO languages such as Java, C++. In those languages, it is the case. The reason is explained (thanks to GPT) in comments below.

@MrCoder
Copy link
Collaborator Author

MrCoder commented Sep 7, 2023

In most programming languages, identifiers (names of variables, functions, classes, etc.) follow certain rules, and one common rule is that identifiers cannot start with a number. There are several reasons for this:

  1. Parsing Clarity: Programming languages are parsed from left to right, and allowing identifiers to start with numbers can create ambiguity in the parsing process. For instance, if numbers were allowed at the start, the statement 123abc=456 would be very confusing: is it a variable assignment or a mathematical expression?

  2. Consistency with Mathematical Notations: In mathematical notation, numerical constants and variables/unknowns are clearly differentiated. Variables are usually represented by letters (and sometimes additional characters), while numerical constants are represented by digits. By not allowing variables to start with a digit, programming languages maintain a separation between constants and variables, which is intuitive and aligns with mathematical conventions.

  3. Readability and Maintainability: Disallowing numbers at the start of identifiers helps maintain the readability of the code. It ensures that developers can easily differentiate between numerical literals and identifiers when reading the code.

  4. Historical and Conventional Reasons: Many programming language syntaxes have evolved from earlier languages, where this convention was established. Newer languages often maintain these conventions to facilitate easier transition for developers familiar with older languages.

  5. Ease of Compilation: Compilers can more easily distinguish between different elements in the code (like variables and numerical literals) when there are clear, established rules for naming. This makes the compilation process more straightforward and less prone to errors or ambiguities.

For all these reasons, Java, like many other programming languages, has adopted the rule that identifiers cannot start with a number. It helps in maintaining a clear, understandable, and error-free syntax and semantics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant