Conditional expressions should not be too complex

Software EleganceCode Complexity

Why you should care

Having complex conditional expressions in your software might slow down your development team to quickly evaluate if and how the code will be run within these conditions. In addition, if conditions are misunderstood because there are too complex (especially when additional native functions like “!empty” or “!isset” makes the whole condition even more complex) , it might create unexpected behaviors during the QA phase or, more badly, in production if the logic path that verifies a condition hasn’t been followed by testers.

Business Impacts

Complex conditional operations can cause loss of time in an attempt to understand the code. It also decreases the innovation capabilities of the code. These factors can make the code unsuitable for clients.

Acceleration
5362

CAST Recommendations

References

Style Guide

How we detect

This code insight verifies that source code doesn’t frequenlty contain too complex logical conditions, by looking at their number of distinct operators (mainly AND and OR). Based on specific thresholds to define whether a condition is complex or not, and depending on the frequency of occurences found in the code, Highlight counts penalty points to the scanned file.

 

Example of a simple conditional expression:
(a && b && c && d && e && f) is not complex, because there is no distinct operators.

Example of a complex conditional expression:
((a && b && c) || (d && e || f)) is complex.

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.

See featuresHow it works