Identifiers and Constant

An identifier is needed to name a variable (or any other entity such as a function or a class). 


C++ imposes the following rules on identifiers:

➤An identifier is a sequence of characters, of up to a certain length (compiler-dependent, typically 255 characters), comprising uppercase and lowercase letters (a-z, A-Z), digits (0-9), and underscore "_".
➤White space (blank, tab, new-line) and other special characters (such as +, -, *, /, @, &, commas, etc.) are not allowed.
➤An identifier must begin with a letter or underscore. It cannot begin with a digit. Identifiers beginning with an underscore are typically reserved for system use.
➤An identifier cannot be a reserved keyword or a reserved literal (e.g.,int, double, if, else, for).
➤Identifiers are case-sensitive. A rose is NOT a Rose, and is NOT a ROSE.
Caution: Programmers don't use blank character in names. It is either not supported, or will pose you more challenges.


Variable Naming Convention

A variable name is a noun, or a noun phrase made up of several words. The first word is in lowercase, while the remaining words are initial-capitalized, with no spaces between words. For example, thefontSize, roomNumber, xMax, yMin, xTopLeft and thisIsAVeryLongVariableName. This convention is also known as camel-case.

Recommendations

  • It is important to choose a name that is self-descriptive and closely reflects the meaning of the variable, e.g., numberOfStudents or numStudents.
  • Do not use meaningless names like a, b, c, d, i, j, k, i1, j99.
  • Avoid single-alphabet names, which is easier to type but often meaningless, unless they are common names like x, y, z for coordinates, i for index.
  • It is perfectly okay to use long names of says 30 characters to make sure that the name accurately reflects its meaning!
  • Use singular and plural nouns prudently to differentiate between singular and plural variables.  For example, you may use the variable row to refer to a single row number and the variable rows to refer to many rows (such as an array of rows - to be discussed later).

Constants are non-modifiable variables, declared with keyword const. Their values cannot be changed during program execution. Also, const must be initialized during declaration. For examples:const double PI = 3.1415926;  // Need to initializeConstant Naming 

Convention: Use uppercase words, joined with underscore. For example, MIN_VALUE, MAX_SIZE.

Comments

Popular posts from this blog

Covid-19 Analysis and Visualization using Plotly Express

Artificial Intelligence (Problem Solving)

Linear Regression