Avoid magic numbers

A magic number is any literal written inline rather than defined as a symbol, except for certain distinguished values such as 1, 0, 10, and so on. For example:

      a &= 0xFFF00000;
      b = 42;
Always define such literals
as identifiers so that you can easily change their values without having to search the code. The most insidious example is sizes of arrays that are hard-coded in the program text. If a change is made to the size of the array, it is difficult to find all the places where it's hard-coded.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker