If a constant value is given for an identifier, do not diminish the maintainability of the code in which it is used by assuming its value in expressions. Simply giving the constant a name is not enough to ensure modifiability; you must be careful to always use the name, and remember that the value can change.
Noncompliant Code Example
This noncompliant example first provides a constant value
$BufferSize set to 512. This constant can later be used to buffer data read in from a file. But the code example defeats the purpose of defining
$BufferSize as a constant by assuming its value in the subsequent expression:
The programmer might assume that everyone knows
$BufferSize equals 512 and that right-shifting 9 bits is the same (for positive numbers) as dividing by 512. But if
$BufferSize changes to 1024 on some systems, the subsequent expression must also be updated and can be overlooked easily. This makes modifications of constants difficult and error prone.
This compliant solution uses the identifier assigned to the constant value in the expression.
Assuming the value of an expression diminishes the maintainability of code and can produce unexpected behavior under any circumstances in which the constant changes.