Identifiers
In order to make our programs more readable, we are allowed to create our own meaningful names for values used in our program. These are known as user-defined symbols (or identifiers) and are used to create named constants, variables and functions. We are allowed to use the alphabetic characters, digits and the underscore character (_) when creating user-defined symbols. User-defined symbols can not start with a digit to avoid confusion with numeric literals.
Symbols are case sensitive meaning that uppercase and lowercase alphabetic characters are considered different characters. For example, the symbols taxrate
, TAXRATE
and taxRate
are three separate and distinct symbols.
Naming Conventions
Because we can use upper and lower case when defining symbols, we often adopt a convention so that it is easy to remember what case is used for our symbols. Common conventions are Camel Case, Pascal Case, All Caps, and Underscore (Unix) case. These styles differ in how long, multi-word symbols are constructed.
Camel Case
When constructing multi-word symbols using the Camel Case convention we run all the words together and nominally use lower case characters. The exception is to use an uppercase character for the first letter in each word starting with the second word. This style became popular in languages such as Java and when writing programs with a GUI. It is often used for variable and function names. Examples of Camel Case symbols are:
- abc123
- costPerUnit
- maxIterations
- taxRate
- uscRule28
- veryLongSymbolName
Pascal Case
This is very similar to Camel Case. The only difference is that with Pascal Case, the first word and all subsequent words are capitalized. This style became popularized by the Pascal programming language and is often used when naming user-defined data types. Examples of Pascal Case symbols are:
- Abc123
- CostPerUnit
- MaxIterations
- TaxRate
- UscRule28
- VeryLongSymbolName
Underscore Case
The Underscore Case is often used in Unix programming. It uses only lower case characters and uses the underscore to separate words in multi-word symbols. For example.
- abc123
- cost_per_unit
- max_iterations
- tax_rate
- usc_Rule_28
- very_long_symbol_name
All Caps
The All Caps style only uses capital letters. It may or may not use the underscore to separate words in multi-word symbols, however using the underscore makes such symbols easier to read. This style is typically used for named constants and not used for variables. For example:
- ABC123
- COST_PER_UNIT
- MAXITERATIONS
- TAXRATE
- USC_RULE_28
- VERY_LONG_SYMBOL_NAME
Questions
- Which of the following are valid identifiers?
- grams_per_mol
- 1st_choice
- fileMenu
- NiñaPintaOrSantaMaria
- CATCH-22
- €To$ConversionFactor
- PI
- LN2