Versions Compared

Key

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

Errors in C, C++, and other programming languages often result when a programmer fails to consider all possible data states. 

Non-compliant Code Example

The following example fails to tests 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.

Code Block
...
if (a == b) {
  ...
}
else if (a == c) {
  ...
}
...

Compliant Solution

The following compliant solution explicitly checks for the unexpected condition and handles it appropriately.

Code Block
...
if (a == b) {
  ...
}
else if (a == c) {
  ...
}
else {
  assert( (a==b) || (a == c) );
  abort();
}
...

Non-compliant Code Example

The following 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.

Code Block
...
switch(a) {
  case: 1
    break;
  ...
}
...

Compliant Solution

The following compliant solution explicitly checks for the unexpected condition and handles it appropriately.

Code Block
...
switch(a) {
  case: 1
    break;
  ...
  default:
     assert( (a==b) || (a == c) );
     abort();
}
...

References

Hatton 95 Section 2.7.2 Errors of omission and addition