Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SonarQube: Qualify Cognitive Complexity

I understand what cognitive complexity is and how it will be calculated, but i don't now how to determine what is a good value for that measure, so how complex my code schouldn't be. I need an objective way to estimate it without to compare project against each other. A kind of formula like "complexity/lines code" or something like that. Or if i define a quality gate for a big project how can i calculate the values for it.

like image 591
Pixel-Killer Avatar asked Jul 13 '17 14:07

Pixel-Killer


People also ask

How does SonarQube determine Cognitive Complexity?

Cognitive Complexity Rule. So we have the response from SonarQube: “Refactor this function to reduce its Cognitive Complexity from 26 to the 15 allowed.” Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain.

How do you determine Cognitive Complexity?

Basic criteria and methodology A Cognitive Complexity score is assessed according to three basic rules: Ignore structures that allow multiple statements to be readably shorthanded into one. Increment (add one) for each break in the linear flow of the code. Increment when flow-breaking structures are nested.

What is a good Cognitive Complexity?

At a method level, 15 is a recommended maximum. At the class level, it depends on what you expect in the package. For instance, in a package that should only hold classes with fields and simple getters or setters, a class with a Cognitive Complexity over 0 (5?

What is an example of Cognitive Complexity?

Cognitive complexity is how complexly or simply people think about a particular issue. So, for example, I may think "broccoli is terrible -- I hate it." That's a pretty simple thought -- one idea about broccoli.


1 Answers

At a method level, 15 is a recommended maximum.

At the class level, it depends on what you expect in the package.

For instance, in a package that should only hold classes with fields and simple getters or setters, a class with a Cognitive Complexity over 0 (5? 10?) probably deserves another look.

On the other hand, in a package that holds business logic classes, a class score >= ... 150(?) might indicate that it's time to look at splitting the class.

In terms of what the limit should be for a project, that's unanswerable, and brings us back to Fred Brooks' essential vs accidental complexity. Basically, there's a certain amount of logic that's required to get the job done. Complexity beyond that is accidental and could theoretically be eliminated. Figuring out the difference between the two is the crux of the issue, and in looking for the accidental complexity I would concentrate on methods where the complexity crosses the default threshold of 15.

To answer your initial question, "What should the limit for an application be?", I would say there shouldn't be one. Because the essential complexity for a simple calculator app is far, far lower than for a program on the Space Shuttle. And if you try to make the Space Shuttle program fit inside the calculator threshold, you're absolutely going to break something.

(Disclosure: I'm the primary author of Cognitive Complexity)

like image 94
G. Ann - SonarSource Team Avatar answered Oct 02 '22 21:10

G. Ann - SonarSource Team