The article talks about how McCabe’s cyclomatic complexity could be used to measure several different aspects of code quality. The objective of this article is to help developers quickly assess code quality by looking at the code. However, let’s try and quickly understand what is cyclomatic complexity and how could it be measured? Thanks for reading it further. And, apologies for spelling mistakes.
Cyclomatic complexity is a measure of code quality that takes into account the number of independent paths through a piece of code. A high cyclomatic complexity indicates that a piece of code is more difficult to understand and maintain, and is, therefore, more likely to contain errors. Cyclomatic complexity can be reduced by breaking down complex pieces of code into smaller, more manageable chunks. This makes the code easier to understand and reduces the chance of introducing errors. In general, cyclomatic complexity should be kept as low as possible to ensure high code quality.
As per Wikipedia definition, Cyclomatic Complexity (CC), developed by Thomas J. McCabe, Sr. in 1976, measures the number of linearly independent paths through a program’s source code. Another page on SonarQube discusses the fact that CC can be measured by adding one (1) to a number of decision paths represented by conditional expressions such as if, while, repeat, for, &&, ||, try-catch, switch-case.
Mathematically, the following is how CC can be calculated:
CC = Number of decision paths (if, while, for, &&, ||, catch, case etc) + 1
Based upon this, try and calculate CC for the following code samples?
Following are some of the related links that could help create the perspective on cyclomatic complexity:
Following are different aspects of code quality that could be measured/predicted by determining the cyclomatic complexity of the code:
Thus, higher the cyclomatic complexity, the probability is that code maintainability may be lower. The code with lower maintainability may be difficult to change and thus, maintain.
If you want to quickly assess different aspects of code quality such as that mentioned above, the thumb rule is to count a number of conditional statements (if, while, catch, for, &&, ||, case, etc) in one or more methods of the class. If the count is very high, it may be sensed that code maintainability and usability are lower, One may need to do code refactoring to achieve a higher degree of maintainability and usability.
All said and done, making a generic statement that the code with high cyclomatic complexity is of poor quality would be incorrect. However, comparing a method with a cyclomatic complexity of 30 with three refactored methods of CC as 10 might make later more maintainable and usable.
In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…
Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…
With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…
Anxiety is a common mental health condition that affects millions of people around the world.…
In machine learning, confounder features or variables can significantly affect the accuracy and validity of…
Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…
View Comments
You mentioned execution paths as an aside, yet, that's where I found cyclomatic complexity to be most useful.
If I remember correctly, in the original paper, McCabe pointed out that by treating each decision point as a node in a graph, you could map out the unique execution paths for test coverage. The idea was to test all possible paths, not to test all possible combinations.
When I tried this manually, I found test cases that I missed just by desk checking my code.
Thanks for your comment, Will