Identifier Naming Rules
Identifier Naming Rules
Acceptable identifiers include names like myIdentifier1, _hiddenValue, and $tempVar. Unacceptable examples are 1stValue (begins with a number), my Identifier (contains space), and int (a keyword).
Following specific conventions for naming identifiers ensures consistency and readability in the code, which facilitates maintenance and collaboration among developers. Proper naming conventions help avoid confusion and potential conflicts in code interpretation and execution .
Not adhering to the no-space rule in identifiers leads to syntax errors, as programming languages generally interpret spaces as separators between tokens. This results in code that is not compilable, therefore inhibiting execution and rendering the code non-functional .
For one-word variable names, lowercase letters are used, e.g., age. Multiple-word variables begin with a lowercase first word, with each subsequent word's first letter capitalized, e.g., ageOfStudent. This structure improves readability and helps developers identify the nature and scope of variables in the code .
If a class name consists of multiple words, the first letter of each word should be capitalized, exemplified by class names like ProgramForDataTypes, JavaDemo, and NumberProgram .
For identifiers, acceptable characters are the alphabetic characters A-Z and a-z, digits 0-9, and special characters like $ and _. An identifier must not begin with a number and should not contain spaces. Also, a keyword must not be used as an identifier .
A method name with a single word should be written in lowercase followed by parentheses, such as run(). For multiple words, the initial word should be in lowercase, followed by capitalized first letters of subsequent words, such as programForDataTypes(). This distinction enhances clarity and minimizes errors by providing a visually recognizable pattern .
Capitalization is crucial as it distinguishes between aspects of an identifier's function. Class names capitalize the first letter per word; methods use lower camel case; variables use lowercase. This impact aids readability and clarity, facilitating conveying meaning and coding standards .
Prohibiting initial numeric characters in identifiers aligns with syntax rules as numbers typically denote values, not names. Allowing them as prefixes would blur the logical demarcation between variables and literals, causing parser confusion and leading to incorrect analysis or execution paths .
Using keywords as identifiers is prohibited because keywords are reserved words in programming languages that have special meanings or functions. If used as identifiers, they would cause conflicts within the code, leading to errors in interpretation and execution .