Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

If one definition affects another, encode the relationship in the definition; do not give two independent definitions. A corollary of this recommendation is not to encode transitory relationships in definitions.

Non-Compliant

...

Code Example

In this non-compliant codingcode example, the definition for OUT_STR_LEN must always be two greater than the definition of IN_STR_LEN. The following definitions fail to embody this relationship:

...

Consequently, a programmer performing maintenance on this program would need to identify the relationship and modify both definitions accordingly. While this sort of error appears relatively benign, it could easily lead to serious security vulnerabilities such as buffer overflows.

Compliant Solution

This pair of definitions The declaration in this compliant solution embodies the relationship between the two definitions.

...

As a result, a programmer could can reliably modify the program by changing the definition of IN_STR_LEN.

Non-Compliant Coding Example

In this non-compliant codingcode example, a relationship is established between two constants where none exits:.

Code Block
bgColor#FFcccc
enum { ADULT_AGE=18 };
enum { ALCOHOL_AGE=ADULT_AGE+3 }; /* misleading, relationship established when none exists */

...