if statements with conditions that are always false have the effect of making blocks of code non-functional. if
statements with conditions that are always true are completely redundant, and make the code less readable.
There are three possible causes for the presence of such code:
- An if statement was changed during debugging and that debug code has been committed.
- Some value was left unset.
- Some logic is not doing what the programmer thought it did.
In any of these cases, unconditional if statements should be removed.
Noncompliant Code Example
if (true) {
doSomething()
}
...
if (false) {
doSomethingElse()
}
Compliant Solution
doSomething()
...
See
- MITRE, CWE-489 - Leftover Debug Code
- MITRE, CWE-570 - Expression is Always False
- MITRE, CWE-571 - Expression is Always True
- MISRA C:2004, 13.7 - Boolean operations whose results are invariant shall not be permitted.
- MISRA C:2012, 14.3 - Controlling expressions shall not be invariant