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:

As explained in ISC++, avoiding literal values usually enhances maintainability.

For obvious reasons, literal numeric values are sometimes called "magic numbers".

if (mode == 23)			// wrong: use an enum instead

Exceptions:

1. Literals are allowed when their meaning is clear from the context, and not subject to future changes.

mean = (a + b) / 2;		// okay, avoid crap like: const int two = 2;

2. String and character literals, e.g. for logging texts.

3. Const array definition, e.g. in case a const array is used to declare a bitmap.

const unsigned int my_bitmap [] = {
	0x00000000u, 0x000000ffu, 0xff000000u, 0x00000000u, 
	0x00000000u, 0x00000fffu, 0xfff00000u, 0x00000000u,
};

4. Well known angles: 90, 180, 270 and 360 are not really magic numbers, these are the four quadrant angles.



Literature References:
Ellemtel Rule 37
ISC++ Rec. 5.4