The code contains too many negative comparisons

Why you should care

This pattern verifies the presence in source code of negative comparisons. Depending on the frequency, CAST Highlight will trigger this code insight.

It’s much easier to think in a positive way about a situation than to be presented with the negative alternative and having to transform it in your mind by yourself to positive. People tend to have a ‘logical’ or ‘the default behaviour’ feeling about true, which makes it easy to think about. On the contrary, false is mostly regarded is the ‘exception’, ‘the error situation’ or the ‘alternative way out’.

Expressing the logic in a positive way helps developers better and quicker understand the code and is less error-prone.

5362

CAST Recommendations

Turn the negative comparison into a positive comparison whenever possible in the source code.

How we detect

CAST Highlight counts one occurrence each time a condition contains “false” keyword or “!” character.

Example:

 

if ( ! (foo == value) { // +1
   var = FALSE;
}
else if(foo == FALSE) { +2
   var = TRUE;
}

5362

About CAST and Highlight’s Code Insights

Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.