Questions tagged [cyclomatic-complexity]

Cyclomatic complexity is a number used to express the complexity of source code (e.g. of a method). It is calculated based on the number of different possible paths through the source code. Low cyclomatic complexity is one factor to improve readability, maintainability, and testability of code.

The Cyclomatic Complexity (or conditional complexity) graphs theoretical software metric to measure the complexity of a program (or method, function, procedure ..). It was defined by Thomas J. McCabe in 1976.

Is calculated using the following formula:

V(G) = P + 1

where V(G) = Cyclomatic Complexity
P = number of decision predicate nodes (if, else, while, etc)

I.e. initially the cyclomatic complexity of each method is defined as 1. Each control flow instruction adds 1 to the complexity number of a method.

Since there is some correlation between good code quality and a low number of complexity, methods with a high Cyclomatic Complexity (e.g. 10 to 15) are considered hard to read and understand. Low number of Cyclomatic Complexity is a good indicator for readable, (re-)usable, reliabe and functional coding style. Because of this, it is recommended to split such methods with a high complexity into smaller ones.

For measuring the needed number of unit tests to get full code coverage it's better to use the Extended Cyclomatic Complexity or the NPath Complexity. Therefore McCabb’s Cyclomatic Complexity is not accurate enough, because it fails to distinguish between different conditional statements (control flow structures). It also does not consider nesting level of various control flow structures.

293 questions
11
votes
1 answer

What is the difference between Cyclomatic Complexity and Essential Cyclomatic Complexity?

There has already been a question on What is Cyclomatic Complexity? However, there is another term called - Essential Cyclomatic Complexity. What are the differences and similarities between these two metrics of the code? What are their typical…
Jay
  • 1,210
  • 9
  • 28
  • 48
10
votes
1 answer

Difference between "complexity" metric and "complexity / method" metric

In Sonar, for a particular Java class, I see : Complexity: 830 Complexity /method: 8,1 How could you explain the difference between those two metrics ? Is "Complexity" the class complexity ? What is the maximum complexity a class should be…
Jérôme Radix
  • 10,285
  • 4
  • 34
  • 40
9
votes
1 answer

javascript: cyclomatic complexity of the wrapper function

Most of my javascript code files look like this: (function() { var Foo = function() { ... }; var Bar = function() { ... }; ... }()); I've tried a number of tools that calculate the cyclomatic complexity of code, and they all…
giorgian
  • 3,795
  • 1
  • 30
  • 48
8
votes
2 answers

Usual values on Code Metrics (C#, Visual Studio) for production projects

There are some questions on Code Metrics here, especially this one on goal values. What I'm looking for though is what's "usual" on real life production projects. Maybe it's just me, but no project I ever get put on ever has these things in mind so…
8
votes
3 answers

Does LINQ and Lambda expressions reduce Cyclomatic-complexity?

Does LINQ and Lambda expressions reduce cyclomatic-complexity? Just curious because CodeRush actually shows a reduction in cc when the VS analyzer increases it.
user24985
  • 765
  • 8
  • 12
8
votes
0 answers

Is there any cyclomatic complexity calculator that takes into account Java stream and reactive APIs?

In a project using spring reactor, we have detected that the real complexity of the reactive code is not detected by our static analysis tools. At the moment we are using a combination of PMD, Checkstyle, Spotbugs and SonarQube, but none of them is…
8
votes
6 answers

Reducing the cyclomatic complexity of a java method

I have the following method: private void setClientAdditionalInfo(Map map, Client client, User user) { Map additionalInfo = (Map) map.get("additionalInfo"); if (checkMapProperty(additionalInfo, "gender")) { …
Akeshwar Jha
  • 4,516
  • 8
  • 52
  • 91
8
votes
2 answers

Calculation of Cyclomatic Complexity for Pseudocode

while(my) AND (a
Ravi Mehta
  • 454
  • 1
  • 8
  • 20
8
votes
1 answer

Cyclomatic complexity, how much is too much?

Just wondering, I have installed CodeMaid for Visual Studio and getting code quality stats on a large codebase. i see numbers ranging from 1 to 300 on many of the methods. How much is too much? Can we (or should we) even have a threshold for…
Ahsan
  • 2,488
  • 2
  • 22
  • 44
8
votes
4 answers

Sonar cyclomatic complexity rule issue - discourages multiple return statements

For the following piece of code, sonarqube computes the method cyclomatic complexity as 9 String foo() { if (cond1) return a; if (cond2) return b; if (cond3) return c; if (cond4) return d; return e; } I understand as per the…
gopalsaob
  • 81
  • 1
  • 1
  • 4
8
votes
3 answers

How can the cyclomatic complexity be 27 in a method with 13 event handler subscriptions?

We have this code, sortof: private void InitializeEvents() { this.Event1 += (s,e) => { }; this.Event2 += (s,e) => { }; this.Event3 += (s,e) => { }; this.Event4 += (s,e) => { }; this.Event5 += (s,e) => { }; this.Event6 +=…
Johann Gerell
  • 24,991
  • 10
  • 72
  • 122
7
votes
4 answers

The Cyclomatic Complexity of this method is greater than authorized

I am using below method for checking null or empty field: public boolean isVoNotNull(){ return null != this.cardNo && StringUtils.isNotBlank(this.cardNo) && null != this.otp && StringUtils.isNotBlank(this.otp) …
Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
7
votes
3 answers

Early return statements and cyclomatic complexity

I prefer this writing style with early returns: public static Type classify(int a, int b, int c) { if (!isTriangle(a, b, c)) { return Type.INVALID; } if (a == b && b == c) { return Type.EQUILATERAL; } if (b == c…
janos
  • 120,954
  • 29
  • 226
  • 236
7
votes
5 answers

What is the best way to reduce cyclomatic complexity when validating data?

Right now I'm working on a web application that receives a significant amount of data from a database that has a potential to return null results. When going through the cyclomatic complexity for the application a number of functions are weighing in…
rjzii
  • 14,236
  • 12
  • 79
  • 119
7
votes
1 answer

How to measure C++ or Java file complexity?

I want to start measuring what Michael Feathers has referred to as the turbulence of code, namely churn vs. complexity. To do this, I need to measure the complexity of a C++ or Java file. So I found a couple tools that measure cyclomatic complexity…
1 2
3
19 20