Categories: Software Quality

Measure Code Quality using Cyclomatic Complexity

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?

Cyclomatic complexity is 5 Courtesy: msdn blog

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. 

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

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.

Recent Posts

Agentic Reasoning Design Patterns in AI: Examples

In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…

1 month ago

LLMs for Adaptive Learning & Personalized Education

Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…

1 month ago

Sparse Mixture of Experts (MoE) Models: Examples

With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…

1 month ago

Anxiety Disorder Detection & Machine Learning Techniques

Anxiety is a common mental health condition that affects millions of people around the world.…

1 month ago

Confounder Features & Machine Learning Models: Examples

In machine learning, confounder features or variables can significantly affect the accuracy and validity of…

2 months ago

Credit Card Fraud Detection & Machine Learning

Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…

2 months ago