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
3
votes
4 answers

What should be the range of cyclomatic complexity for JAVA

I want to know that what should be the standard range of cyclomatic complexity? And does it depends on language or platform or it is common for all? I am using State of Flow - EclipseMetrics which is a Eclipse plugin calculates various metrics for…
Nirali
  • 13,571
  • 6
  • 40
  • 53
3
votes
1 answer

Cyclomatic complexity of logically similar code

Consider the following three functions, which all behave in an identical manner, using different code to achieve the same thing (examples are written in JavaScript, and I'm particularly interested in answers as applied to JavaScript, but this…
James Allardice
  • 164,175
  • 21
  • 332
  • 312
3
votes
3 answers

How to reduce Cyclomatic complexity in an if-else statement

What will you do in this case to reduce the Cyclomatic Complexty if (Name.Text == string.Empty) Name.Background = Brushes.LightSteelBlue; else if(Age.Text == string.Empty) Age.Background = Brushes.LightSteelBlue; else if(...) …
Wassim AZIRAR
  • 10,823
  • 38
  • 121
  • 174
2
votes
1 answer

Understanding Cyclomatic complexity of a simple condition

I am trying to understand the cyclomatic complexity concept and came across in web that the cyclomatic complexity for below code (pseudo) is 3, but I thought, as there are 4 possible test scenarios, cyclomatic complexity has to be 4 If (Condition…
Jay
  • 21
  • 2
2
votes
1 answer

How to decrease cyclomatic complexity of the multiple conditions in if?

I have been trying to learn how to reduce cyclomatic complexity. I have here 5 or more conditional checks inside a if statement e.g if(something1() && something2() && something3() && something4() && something5()){ doThatThing(); }else{ …
2
votes
2 answers

How to reduce cyclomatic complexity on this c# method?

I'm currently doing a project for my c# classes. Our teacher gave us some code metrics limits that we have to abide to and one of them is cyclomatic complexity. Right now he complexity of the method below is 5, but it needs to be 4. Is there any way…
2
votes
1 answer

Ruby Cyclomatic and Perceived complexity

I have this service object that parses an excel file and persists the data in it. Rubocop is flagging it as: app/services/csv_upload.rb:17:3: C: Metrics/AbcSize: Assignment Branch Condition size for parse_spreadsheet is too high. [<14, 34, 11>…
2
votes
1 answer

Sonar doesn't increment cyclomatic complexity score for 'forEach' loops in java

Suppose that I have the following code. map.forEach((key, value) -> { //No Sonar cyclomatic complexity score for (String s : value) { //Sonar lint : +2 (including 1 for nesting) if (condition) {...} //Sonar lint : +3 (including 2 for…
Insightcoder
  • 506
  • 4
  • 14
2
votes
3 answers

Improve the cyclomatic complexity code for if-else statements

I have a block of if-else statements in a method for which I am getting cyclomatic complexity issue. I have tried using switch statement for this but still the issue remains. if (fileName.startsWith(HwErrorsEtlConstants.MR_RAW_GRADIENT_ERRORS)) { …
Abhilash
  • 803
  • 1
  • 9
  • 32
2
votes
2 answers

How do i express Try/Catch in a control flow graph?

I'm trying to calculate some Cyclomatic Complexity, hence trying to draw a Control Flow Graph. Firstly i'm trying to make it for a fairly simple method. Firstly i tried drawing it for just the try part like this: Heres the method: [HttpPost] …
Anders Jensen
  • 330
  • 3
  • 20
2
votes
2 answers

Computational complexity for regular expressions

Regular expressions rapidly become too complex (for me) to understand. Even something so simple as [ab][cd], has several logical branches. My goal is to improve the maintainability of our code base, so answers to these questions could help us…
2
votes
1 answer

Effective way of checking a string for occurrences of other strings and changing the string accordingly using C#

I need a method to effectively search strings for specific patterns, and changing the string based on the pattern that is found. Right now, my solution works, but I'm not happy with it at all due to a high cyclomatic complexity. What I've got is…
StefRoo
  • 21
  • 3
2
votes
1 answer

Reducing cyclomatic complexity of object oriented python code

I am trying to reduce cylomatic complexity of code, because according to pylama my definition is 'too complex' and suggested solution includes calling functions with dictionary mappings. So I tried it on my object oriented code but failed…
Chinmaya B
  • 405
  • 1
  • 7
  • 21
2
votes
3 answers

Alternative for too many switch case for cyclomatic complexity reduction?

I have a function for checking the type of a $value is valid or not. My current code is a simple switch case with too many cases exceeding the cyclomatic complexity to 17. I need to add more cases as well as reduce the complexity. /** * Check type…
2
votes
1 answer

Calculate Cyclomatic complex when it comes to a recursion function

Traditionally, Cyclomatic Complexity(CC) can be obtained by using the way that the number of "if-else" and then plus one. But when it comes to a recursion function, I found I unable to figure out the number of "if-else". More specifically, in this…
Spica
  • 23
  • 6