Now a days i switched to sonar reports for static code review and performance improvement. Under the rules section I found that the cognitive complexity of my methods are high.
You can find cognitive complexity error in sonar as: Go to Project->Issues Tab->Rules Drop-down->Cognitive Complexity
Below screen shot gives you a reference of sonar project:
I was not getting any way to calculate and reduce the cognitive complexity of my methods. Finally I found the accurate way to calculate the complexity and i will answer this in my post below. Please check out.
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.
Cognitive complexity How easy or difficult it is for a human being to understand the purpose and behavior of a piece of code. SonarQube defines a cognitive complexity of 15 as the default threshold for functions. This is a good starting point.
To estimate the cognitive complexity we must first calculate the behaviour complexity ("BC") of each user and each task. Then we estimate the task complexity ("TC") of each task by searching for the minimum of the 12 empirical values of the behaviour complexity (the "best" solution).
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.
Cognitive Complexity
After searching some blogs and having chat with sonar team I found an easy definition and calculation of cognitive complexity which is as below:
Definition:
Cognitive Complexity, Because Testability != Understandability
Your written code must be as simple to understand as the above definition, simple.
less Cognitive Complexity more Readability
Let's see a method for example to calculate CC, right now I am referring kotlin language, see below image:
In above image there is a method getAppConfigData()
, whose cognitive complexity is being measured. Right now the CC of this method is 18. As you can check in above screen shot there is a warning, which tells that the limit of maximum complexity is 15, which is lower than the current CC of this method.
Now the actual question is: How can I calculate the CC of my method at the time of development?
Follow below rules to get your CC of any method or class as:
So whenever above rules matches, just add + count to your CC and remember count will be increased according to level of code break, as example "if" condition gets +1 if it is the first code break but if you have used one more nested if then it will be a +2 for that inner "if" as shown in below image.
That's all I got in terms of Cognitive Complexity.
You can find everything related to CC at sonar blog
Thank You
More explained answer in Sonar 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
Additionally, a complexity score is made up of four different types of increments:
- Nesting - assessed for nesting control flow structures inside each other
- Structural - assessed on control flow structures that are subject to a nesting increment, and that increase the nesting count
- Fundamental - assessed on statements not subject to a nesting increment
- Hybrid - assessed on control flow structures that are not subject to a nesting increment, but which do increase the nesting count
While the type of an increment makes no difference in the math - each increment adds one to the final score - making a distinction among the categories of features being counted makes it easier to understand where nesting increments do and do not apply. These rules and the principles behind them are further detailed in the following sections.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With