Errors in C, C++, and other programming languages often result when a programmer fails to consider all possible data states.
Non-Compliant Code Example
This example fails to test for conditions where a
is neither b
nor c
. This may be the correct behavior in this case, but failure to account for all the values of a
may result in logic errors if a
unexpectedly assumes a different value.
... if (a == b) { ... } else if (a == c) { ... } ...
Compliant Solution
This compliant solution explicitly checks for the unexpected condition and handles it appropriately.
... if (a == b) { ... } else if (a == c) { ... } else { assert( (a == b) || (a == c) ); abort(); } ...
Non-Compliant Code Example
This example fails to consider all possible cases. This may be the correct behavior in this case, but failure to account for all the values of a
may result in logic errors if a
unexpectedly assumes a different value.
... switch(a) { case: 1 break; ... } ...
Compliant Solution
This compliant solution explicitly checks for the unexpected condition and handles it appropriately.
... switch(a) { case: 1 break; ... default: abort(); } ...
References
Hatton 95 Section 2.7.2 Errors of omission and addition