4.2: Identifier Names
- Page ID
Within programming a variety of items are given descriptive names to make the code more meaningful to us as humans. These names are called "Identifier Names". Constants, variables, type definitions, functions, etc. when declared or defined are identified by a name. These names follow a set of rules that are imposed by:
- the language's technical limitations
- good programming practices
- common industry standards for the language
Technical to Language
- Use only allowable characters (for C++ the first character alphabetic or underscore, can continue with alphanumeric or underscore)
- Can't use reserved words
- Length limit
These attributes vary from one programming language to another. The allowable characters and reserved words will be different. The length limit refers to how many characters are allowed in an identifier name and often is compiler dependent and may vary from compiler to compiler for the same language. However, all programming languages have these three technical rules.
Good Programming Techniques
- Be case consistent
Meaningful identifier names make your code easier for another to understand. After all what does "p" mean? Is it pi, price, pennies, etc. Thus do not use cryptic (look it up in the dictionary) identifier names.
Some programming languages treat upper and lower case letters used in identifier names as the same. Thus: pig and Pig are treated as the same identifier name. Unknown to you the programmer, the compiler usually forces all identifier names to upper case. Thus: pig and Pig both get changed to PIG. However not all programming languages act this way. Some will treat upper and lower case letters as being different things. Thus: pig and Pig are two different identifier names. If you declare it as pig and then reference it in your code later as Pig – you get a compiler error. To avoid the problem altogether, we teach students to be case consistent. Use an identifier name only once and spell it (upper and lower case) the same way within your program.
- Do not start with underscore (used for technical programming)
- variables in all lower case
- CONSTANTS IN ALL UPPER CASE
These rules are decided by the industry (those who are using the programming language). The above rules were commonly used within the "C" programming language and have to large degree carried over to C++.
- reserved word
- Words that cannot be used by the programmer as identifier names because they already have a specific meaning within the programming language.