TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
C++ Coding Standard
Search

Rule:  OLC#009Checked automatically with code checker

Synopsis:Literals should be used only on the definition of constants and enumerations
Language:C++
Severity Level:7
Category:Object Life Cycle


Description:

Avoiding literal values usually enhances maintainability and readability. Uncommon literal numeric values are called "magic numbers". Having 2 or more identical magic numbers in the same scope is dangerous. If one needs to change the magic number there is a risk that one forgets to change the others.

Wrong example:

if (mode == 23) {
  throw MyException(23);
}

Correct example:

const int IncorrectState = 23;

if (mode == IncorrectState) {
  throw MyException(IncorrectState);
}

Note that the following numbers are considered to be no magic numbers: 0, powers of 2, powers of 10 and the well known angles: 90, 180, 270 and 360. Another exception concerns const array definitions.