Avoid long case statements

Why you should care

The switch statement should be used only to clearly define some new branches in the control flow. As soon as a case clause contains too many statements this highly decreases the readability of the overall control flow statement. In such case, the content of the case clause should be extracted into a dedicated method.

How we detect

CAST Highlight calculates the average lentgh of all “case” blocks in a source file. Depending on some benchmark thresholds, this code insight will be triggered.

The length is expressed in number of lines. The first line taken into account is the line of the “case” keyword, and the last is the line of the last instruction.

References

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.