How Readable Is Your Code? Part 2
In the first part, I've introduced Cyclomatic Complexity (CYC) metrics in the previous part. So CYC has no intention to be a readability code metric.
Join the DZone community and get the full member experience.Join For Free
Once Again About Cyclomatic Complexity
In the first part, I've introduced Cyclomatic Complexity (CYC) metrics in the previous part. So CYC has no intention to be a readability code metric. CYC was invented a long time ago (in 1976) by Thomas J. McCabe.
Cognitive Complexity Metric.
Cognitive Complexity (CC) attempts to count the cognitive effort required to understand the code's flow. Let's take a look at the next example:
Both methods have the same CYC equal to 4. But the code on the right side is much easier to read.
Cognitive Metric Count Rules
The cognitive complexity (CC) metric has a couple of rules. Some of them reduce complexity (compare to Cyclomatic), but some of them increase it. Let's take a lot at each of them using examples.
Ignoring Shorthand (E.g., Null Check) — Reduced Complexity
Now languages provide syntax features that allow you to write multiple statements into one. For such cases, CG doesn't increment complexity. E.g., a null check can be reduced with optional:
For both cases, CYC (Cyclomatic Complexity) is 2, but CC (Cognitive Complexity) is 1.
Switches — Reduced Complexity
All switch case operators are counted as 1. For the next example, CYC is 4 and CC is 1.
Sequences of Logical Operator — Reduced Complexity
Cognitive Complexity doesn't increment for each binary logical operator. Instead, it assesses a fundamental increment for each sequence of binary logical operators.
In the next example, CYC is 5 and CC is 2; all sequential logical operators are not considered.
In this example, CYC is still 5, but CC is 3, the last condition is changed, so it's counted.
Nested Flow Breaks Structures — Increased Complexity
Nested code is much difficult to read than the same code as a liner series. A mix of different code structures like try, for, while, if together increase complexity significantly. This why Cognitive Complexity for nested structures adds a point. In the next example, CYC is 4, and CC is 6! So 1 additional point added for nested for and 2 additional points for twice nested if.
How To Calculate Cognitive Complexity for You Java Code?
SonarQube platform for code quality inspection allows you to calculate Cyclomatic and Cognitive Complexity for your code. To do it, follow the next steps:
2. Start it with StatSonar.bat (You need java 11 preinstalled)
3. Create your maven based project and execute the maven command in the root folder:
mvn sonar:sonar -Dsonar.host.url=http://localhost:9000
4. Open admin tool in browser with http://localhost:9000/ (credentials: admin/admin)
5. Find your project and Check Complexity in Measures tab:
Cognitive Complexity White Paper
The complete manual about Cognitive complexity you can find here.
Introduced metrics are practical tools that might help you to understand and improve the quality of your code. If refactored code performs the same functionality, you likely made your code more readable and clean with reduced CYC and CC.
Published at DZone with permission of Dmitry Egorov. See the original article here.
Opinions expressed by DZone contributors are their own.