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
7
votes
2 answers

Cyclomatic complexity count of 31, where has this come from?

I'm developing an application that pulls data from an Excel file (I don't have access to the actual database) and I've written a method which has as its only function to pull the data from the Excel Spreadsheet, as seen below. private…
Jake Hendy
  • 303
  • 3
  • 18
6
votes
0 answers

Why aren't switch expressions counted in cyclomatic complexity?

Let's say I have this method: void DoStuff(int a) { int b; switch (a) { case 1: b = 5; break; case 2: b = 100; break; default: b = 0; …
Nigel
  • 2,961
  • 1
  • 14
  • 32
6
votes
1 answer

Cyclomatic Complexity in Visual Studio

I do some tests with Visual Studio Code Metrics. As I can calculate the Cyclomatic Complexity, each if, while, for - operators increase the complexity with 1. I have the next simple method: static bool ContainsNegative(int a, int b, int c, int d) …
Pepo
  • 135
  • 1
  • 10
6
votes
2 answers

How does Sonar calculate the cyclomatic complexity?

Sonar gives me the following cyclomatic complexity number : 22. For the following program : private static SomeDto checkSomething(AnotherDto anotherDto, String reference) { SomeDto someDto = new SomeDto(); // condition 1 if (!someDto.getA()) …
6
votes
1 answer

Why does list initialization with lambda causes high cyclomatic complexity?

Initialization of list with lambdas causes high IL cyclomatic complexity: why, and how remove this complexity? For example following code causes the static constructor of the class (which is actually compiler generated) to be very complex: 1 + the…
sthiers
  • 3,489
  • 5
  • 34
  • 47
6
votes
3 answers

Can high cyclomatic complexity (warnings) be avoided when using switch-case on a large enum?

Assume a method chooses an action depending on a value from a fairly large enum. Our Sonar now complains about a high cyclomatic complexity (of about the number of case statements, naturally) of this method. I know, large switch case statements are…
Uwe Allner
  • 3,399
  • 9
  • 35
  • 49
6
votes
3 answers

Cyclomatic Complexity in piece of code with multiple exit points

I have this method that validates a password: /** * Checks if the given password is valid. * * @param password The password to validate. * @return {@code true} if the password is valid, {@code false} otherwise. */ public static boolean…
kelo
  • 489
  • 1
  • 11
  • 20
6
votes
1 answer

Cyclomatic Complexity With Compound Conditions and Short Circuiting

I am studying Cyclomatic Complexity in my Software Quality Assurance course at the University and I am having a hard time understanding how it works when you have compound conditions in a predicate statement or node. I have seen multiple definitions…
Cory Gross
  • 36,833
  • 17
  • 68
  • 80
6
votes
2 answers

Cyclomatic complexity of IF((A>B) AND (C>D)) and IF((A>B) OR (C>D))

I want to know the cyclomatic complexity of two section of code, IF((A>B) AND (C>D)) { a=a+b;c=c+d;} as far my knowledge the cyclomatic complexity of above code=2+1=3, Another code IF((A>B) OR (C>D)) {a=a+b;c=c+d;} Complexity of the above code…
Imran
  • 73
  • 1
  • 1
  • 6
6
votes
3 answers

Tools to automate calculation of cyclomatic complexity in java?

Are there any tools available for Java that can automagically determine the cyclomatic complexity of given Java code? I have sought out tools online, and have yet to find one.
mjgpy3
  • 8,597
  • 5
  • 30
  • 51
5
votes
6 answers

why would I refactor this code as Cyclomatic Complexity is 58

I read that having CC 10 or less would be highly maintainable code. But the method that I wrote have CC 58. Thanks to VS 2010 code analysis tool. I believe that the method I wrote is very simple, readable and maintainable as far as my understanding.…
Amzath
  • 3,159
  • 10
  • 31
  • 43
5
votes
4 answers

Deriving Cyclomatic Complexity in .NET

I know that I can access the cyclomatic complexity to my code in Visual Studio 2008 Team Explorer by right clicking and selecting "Calculate Code Metrics". I would like to expose this data for a web application to display it. Does anybody know of…
pixelbat
5
votes
5 answers

Would you abstract your LINQ queries into extension methods

On my current project we set ourselves some goals for the code metrics "Maintainability Index" and "Cyclometic Complexity". Maintainability Index should be 60 or higher and Cyclometic Complexity 25 or less. We know that the Maintainability Index of…
5
votes
6 answers

Cyclomatic complexity rightfully reduced by using private methods?

Using private methods for decreasing CC by refactoring some decision points into separate methods decreases the CC of the actual method and eases reading, but does not decrease the effort to get full branch coverage in testing. Is this justifyable?…
ron
  • 9,262
  • 4
  • 40
  • 73
5
votes
3 answers

Cyclomatic Complexity reduction

I have a block of code that I am having an issue reducing the cyclomatic complexity of. Because of the multiple conditions that have to match, I am not sure the best way to break it down further. Complicating matters is that in 2 of the cases a…
cicit
  • 581
  • 5
  • 24