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.
What is Cyclomatic Complexity?
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:
How could Cyclomatic Complexity help measure or rather predict Code Quality?
Following are different aspects of code quality that could be measured/predicted by determining the cyclomatic complexity of the code:
- Maintainability: One of the key characteristics of code maintainability is its testability. The higher the testability, the higher the code maintainability. Testability can be measured in terms of unit test coverage (code coverage). The higher cyclomatic complexity of a method represents the greater number of conditional statements leading to the greater number of execution paths. The greater the number of execution paths in a method, difficult is it to achieve higher code coverage. Thus, greater the cyclomatic complexity of the method, it can be comprehended that the testability of the code may be lower, thus, leading to lower maintainability. Following is mathematical representation: Let Maintainability be represented by M, Testability by T, Cyclomatic Complexity by CC and, Number of execution paths by N.CC N,M T,T 1 / N,
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.
- Usability: Usability of the code is represented by read-ability and understand-ability of the code. And, higher cyclomatic complexity ( and thus, greater execution paths or conditional statements) may lead to lower read-ability and understand-ability of code, and thus, lower code usability. And, lower code usability may make it difficult for the developer to change due to difficulty in reading and understanding the code.
How to quickly assess different aspects of code quality such as maintainability and usability?
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.