Code Metrics Cyclomatic Complexity

Background

As a software developer, we perform coding in any programming language. Once we are done with the coding part, we do assume that our part is over. But, that's not true in a real scenario. Our job does not end here. We need to look back to our code and take care of the below points.

  1. Keep your code simple to understand.
  2. Less complexity while using If…else statement or Switch case or any conditional statement.
  3. Is this code manageable in future for any kind of changes or new development?

Introduction to Cyclomatic Complexity

Cyclomatic complexity is a software metric (measurement) used to indicate the complexity of a program. It is a quantitative measure of the number of linearly independent paths through a program's source code. It was developed by Thomas J. McCabe, Sr. in 1976.

In General, Cyclomatic Complexity tells how complex your code is.

Based on the numbers given per method in the source code, one can easily tell if the code is complex or not. The cyclomatic complexity also affects other software metrics, like code maintainability index. Essentially, with cyclomatic complexity, higher numbers are “bad” and lower numbers are “good”.

The cyclomatic complexity is more in the classes/methods where there are a lot of conditional operators (e.g if..Else, while, switch statements).

The number of lines in a class or a method also affects the cyclomatic complexity. The higher number of lines means the combination of several logics altogether, which clearly violates the SRP (single responsibility principle).

Cyclomatic complexity helps us by measuring the code complexity. The higher the code complexity, the more complex is the code. The number of the Cyclomatic complexity depends on how many different execution paths or control flow of your code can execute depending on various inputs.

Acceptable metrics for Cyclomatic Complexity

Score Cyclomatic Risk Type
1 to 10 Simple Not much risk
11 to 20 Complex Low risk
21 to 50 Too complex Medium risk, attention
More than 50 Too complex, Can't test , high risk

Advantages

  • Easy to find complex code for formal review.
  • Help focus testing efforts
  • Easy to compute and maintenance code in future.
  • Easy to compute as illustrated in the example.

Let’s see it in action. Create a new console application and immediately calculate your code metrics by going to Analyze | Calculate Code Metrics for Solution.

  1. Open Visual Studio. File - New - Project.

    Cyclomatic Complexity

  1. Right-click on project and select ‘Calculate Code Metrics’.

    Cyclomatic Complexity

  1. Let’s check its cyclomatic complexity.

    Cyclomatic Complexity

    Notice that the cyclomatic complexity is at 2 (the lowest value possible). If I add non-decision code, the complexity doesn’t change.

    Cyclomatic Complexity

  1. Add a decision, the cyclomatic complexity value goes up by 1.

    Cyclomatic Complexity

Conclusion

I hope you have got an idea of cyclomatic complexity. I hope you enjoyed this article. Your valuable feedback, questions, or comments about this article are always welcome.