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

Better way to write methods that return methods based on a set of conditions in Ruby

I've run into a problem with the Cyclomatic complexity being too high for this ruby method: def find_value(a, b, lookup_value) return find_x1(a, b) if lookup_value == 'x1' return find_x2(a, b) if lookup_value == 'x2' return find_x3(a, b) if…
QCFia
  • 39
  • 4
2
votes
1 answer

Cyclomatic Complexity - Cobol

I have to calculate Cyclomatic Complexity for a Cobol program that contains only an EVALUATE like this one: EVALUATE x WHEN x<0 ... WHEN x=0 ... WHEN x between 1 and 10 ... WHEN OTHER ...` END EVALUATE.` I have also to calculate…
Fede
  • 21
  • 1
2
votes
0 answers

generating cyclomatic complexity graphs

(Before marking this as duplicate I have searched a lot and not found what I am looking for) A tool that generates control flow graphs from java source code. Which would then allow me to calculate cyclomatic complexity. (I don't mind if it does both…
Mark
  • 1,337
  • 23
  • 34
2
votes
1 answer

How can I reduce the cyclomatic complexity of a function that returns a value dependent on the cartesian product of 3 booleans?

How can I reduce the cyclomatic complexity of a function that returns a value dependent on the cartesian product of 3 booleans? How can I make the following code look more clean? This is for a school project, and is not really a requirement for the…
Emil Rosenius
  • 961
  • 1
  • 9
  • 24
2
votes
2 answers

Javascript: Reduce cyclomatic complexity

function(response) { if (response.bMap && SelType === 'q') { setDefaultQQ(response.bMap); } else if (response.bMap && SelType === 'a') { setDefaultAA(response.bMap); } else if (response.bMap && SelType === 'o') { …
2
votes
3 answers

How to reduce cyclomatic complexity for these if else statements

I am trying to validate the command line arguments and print an error message if there is some error. My problem is that if the number of command line parameters is increased (currently, I only have 3), then my code would turn into spaghetti code.…
Akiner Alkan
  • 6,145
  • 3
  • 32
  • 68
2
votes
1 answer

Clarifying the manual count of Cyclomatic Complexity

Let's assume that we have a code like this: switch(y) { case 1: case 2: case 3: function(); break; case 4: case 5: case 6: function_2(); break; } Can we get the CC value as 6+1 here? Why a value of 1 is added? If the CC value is…
Spark
  • 47
  • 5
2
votes
2 answers

Reduce complexity of 14 for a method

Pmd told me this method (thirdRowsValidation) has a complexity of 14 but I cannot reach a patter to reduce the code. indexBookngEnd, indexTravelStart ... all those variables are indexes from other array made in another loop iteration (Header of the…
arnoldssss
  • 468
  • 3
  • 9
  • 22
2
votes
2 answers

C# :Cyclomatic Complexity of a method with FxCop sdk

I need to calculate the cyclomatic complexity of C# methods and need to define the rule according to the CC value, in the FXcop 12.0. I've found that the tools like Code Metrics provide functionality to calculate the CC values, but I don't know how…
2
votes
1 answer

Implementing a Metric Suite using ASTParser in Java

Background - Question below I am at the start of implementing a metric suite in Java for Java however I am concerned that my approach is not appropriate. Currently I am using the JDT's ASTParser for every file within a directory. This started off…
2
votes
3 answers

Average Cyclomatic Complexity of multiple files

I ran a static code analysis on a couple of projects and got the Cyclomatic Complexity for every file in those projects from the report that was generated. Now I want to calculate the average Cyclomatic Complexity for the whole project. How would I…
2
votes
2 answers

Cyclomatic complexity paths count

I'm reading a book about testable JS and there is a chapter about Cyclomatic complexity, but it doesn't really tell how to calculate it. It just says that Cyclomatic complexity is a measure of the number of independent paths through your…
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
2
votes
1 answer

Is the Cyclomatic complexity value shown by Visual studio ILCyclomatic complexity

I have ran code analysis on my solution using Visual studio 2012 and NDepend For a method MethodA Visual studio shows the complexity as 105 and Ndepend shows it as 12. However the ILCyclomatic complexity in NDepend is 112. Does this mean that the…
2
votes
2 answers

Maximal cyclomatic complexity for Angular apps

I am wondering what Cyclomatic Complexity should have an angular app (each of it's controller, service, etc.) to be easily testable and maintainable. I have found in Microsoft Documentation that Visual Studio has set it's warning level at 25 but I…
2
votes
1 answer

Cyclomatic complexity of McCabe vs Yourself Drawing

So pretty much I was taught to work out the cyclomatic complexity like this site does But then recently I found this thing that says Cyclomatic Complexity = ( 1 + ifs + loops + cases ). Are they the same? is the thing I read perfect for every case?…