The article talks about how McCabe cyclomatic complexity could be used to measure several different aspects of code quality. The objective of this article is to help developer quickly assess the 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?
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 number of decision paths represented by conditional expressions such as if, while, repeat, for, &&, ||, try-catch, switch-case. Mathematically, following is how CC can be calculated:
CC = Number of decision paths (if, while, for, &&, ||, catch, case etc) + 1
Based upon, try and calculate CC for 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. Higher the testability, higher is the code maintainability. Testability can be measured in terms of unit test coverage (code coverage). Higher cyclomatic complexity of a method represents the greater number of conditional statements leading to greater number of execution paths. Greater the number of execution paths in a method, difficult is it to achieve higher code coverage. Thus, greater the cyclomatic complexity of method, it can be comprehended that 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,
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 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 is lower, One may need to do code refactoring to achieve 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 cyclomatic complexity of 30 with three refactored methods of CC as 10 might make later more maintainable and usable.
- When to use Deep Learning vs Machine Learning Models? - January 17, 2021
- Most Common Types of Machine Learning Problems - January 14, 2021
- Historical Dates & Timeline for Deep Learning - January 10, 2021