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

Understand a JavaScript function's cyclomatic complexity

I want to fix This function's cyclomatic complexity is too high messages and stumble on a simple function which contains only a switch statement. The function's cyclomatic complexity was calculated to (5)…
Stephan Ahlf
  • 3,310
  • 5
  • 39
  • 68
0
votes
0 answers

Calculating Cyclomatic Complexity

So I'm trying to understand cyclomatic complexity. If I have a line of code that looks like a = b && c where a, b, and c are bool. For this statement would the complexity be 1 or would it be more like doing if statements since you have to check b…
Morgan
  • 1
  • 1
0
votes
1 answer

Cyclomatic Complexity number - do I have to count every statement separately as node?

I came across different ways of calculating CCN (according to formula CCN = E-N+2P) One way was to count all the lines in the code separately and the other way is to count a few lines of code as one step; lets have the following example: 1 public…
L1l1th
  • 1
  • 1
0
votes
1 answer

How to reduce this Cyclomatic complexity in this JavaScript code?

I'm building a simple ETL tool using Node.js. So, I get one json object and manipulate to another object. However, after running through Lint, I get 18:1 warning Function 'format' has a complexity of 5 complexity This is the example of the…
toy
  • 11,711
  • 24
  • 93
  • 176
0
votes
1 answer

How do I improve cyclomatic complexity of some options?

So do I just ignore this code analysis warning by suppressing it? Or is there a way to truly fix it? Here is a user story that comes close to mine, but I changed it slightly so that company information isn't on the site... Say I have a website for a…
KarlZ
  • 170
  • 9
0
votes
1 answer

Klocwork Insight complexity for specific modules

The Insight Review allows the user to view the 15 most complex methods. I wish to see the complexity metrics for a specific C++ Visual Studio project and its component modules. Earlier Insight user interfaces provided this. There was a directory…
Robert R Evans
  • 177
  • 1
  • 11
0
votes
1 answer

Cyclomatic Compleity IF ELSE vs SWITCH CASE

Many code analysis tools like sonar throw errors for switch case if used instead of multiple is-else for cyclomatic complexity . Is there a number which should be a threshold of when one should use if else and when one should use switch case ? Say…
0
votes
1 answer

Java Code optimization : if else to resolve PMD complexity 24 to 0

I have string of array with size already defined and i have to iterate though this array and fill it with fields value of someObject I have . If i do this with if elseif elseif or switch case i get high complexity .. please kindly suggest…
0
votes
2 answers

When calculating cyclomatic complexity, should statements exiting the current method/function be part of it?

This probably applies to more than C and Java, of course, but those are the two languages I am more familiar with, so let us take this simple example: int foo(int arg) { if (arg > 0) return 1; return 0; } Now, I have seen two…
fge
  • 119,121
  • 33
  • 254
  • 329
0
votes
1 answer

Cyclomatic complexity error in my code

I am getting 2 errors in my grunt file line 61 col 25 This function's cyclomatic complexity is too high. (10) line 101 col 22 This function's cyclomatic complexity is too high. (10) how could I reduce the Cyclomatic complexity in this case ? my…
Non
  • 8,409
  • 20
  • 71
  • 123
0
votes
1 answer

Best way for Cyclomatic complexity reduction

Below Method has cyclomatic complexity of 13 i follow some approach using ternary operator for small if else, but for long code what is best approach what design pattern use for resuce if else condition or any other way to reduce cyclomatic…
vaib
  • 319
  • 4
  • 27
0
votes
2 answers

Reduce Cyclomatic complexity while processing DataRows

I have the following piece of code which checks if a particular DataRow has a column of a particular name and if it is not NULL. private static bool HasValue(DataColumn c, DataRow row) { if (c != null && row != null && row[c.ColumnName] !=…
ckv
  • 10,539
  • 20
  • 100
  • 144
0
votes
1 answer

Does McCabe formula consider data flow?

When using the McCabe formula M = E-N + 2C does it take into consideration if data is restricted to flowing in only one direction? It seems graphs show data flowing in many directions, regardless if that is actually what happens or not. A…
4thSpace
  • 43,672
  • 97
  • 296
  • 475
0
votes
3 answers

Reducing the Cyclomatic Complexity of the code

Sonar gives a major violation error ("Cyclomatic Complexity") for the following code. Following method is used to get the date in a special format, e.g. 14-02-3 (Year-month-weekid). How can I overcome this violation? private String…
Amila Iddamalgoda
  • 4,166
  • 11
  • 46
  • 85
0
votes
1 answer

Simple Cyclomatic Complexity Calulations

I found this site claiming that Cyclomatic Complexity = ( 2 + ifs + loops +cases - return ) and I also found out that Cyclomatic Complexity can be calculate by the number of conditional statement + 1 which is basically the same. Now the above states…
rikket
  • 2,357
  • 7
  • 46
  • 74