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
1 answer

Cyclomatic complexity in dotCover report in Teamcity

I'm using TeamCity 8.1.3 (currently the latest version) that includes dotCover 2.6 for free Love the simplicity of dotCover setup! But can't figure out how to get cyclomatic complexity metric into TeamCity. As far as I understand dotCover shows…
Ivan
  • 9,089
  • 4
  • 61
  • 74
2
votes
3 answers

Cyclomatic complexity of a code with multiple exit points

How to find the cyclomatic complexity of a function with multiple exit points? The wiki page says p-s+2 where p is the number of decision points and s is the number of exit points. But should not more exit points increase the cyclomatic…
Aman Neelappa
  • 145
  • 1
  • 1
  • 9
2
votes
2 answers

java switch cyclomatic complexity vs performance

I have a case here where the switch statement contains about 40 cases of each returning a different configured object based on input. This method is shown as having too high cyclomatic complexity in the metrics and usually I would change this into a…
Kai
  • 2,145
  • 1
  • 20
  • 35
2
votes
3 answers

What is the principle behind calculating the complexity of methods?

From Sonar Metrics complexity page the following method has a complexity of 5. public void process(Car myCar){ <- +1 if(myCar.isNotMine()){ <- +1 return; <- +1 } …
Geek
  • 26,489
  • 43
  • 149
  • 227
2
votes
0 answers

CyVis 0.9 Ant task failing to generate html report

I am currently attempting to make an Ant build file for a Java project that uses CyVis 0.9 as one of its tools. I am running into a problem where I cannot get it to generate an html report. Below is the exact code from my build.xml file, which is…
Jason
  • 23
  • 4
2
votes
2 answers

Cyclomatic complexity in Legacy VB6.0 application

Suggest any tool that should support to VB6.0 I am looking for following metrics Code Analysis Code Maintainability Index Cyclomatic complexity
2
votes
1 answer

McCabe's Complexity Metric and Independent Paths

int maxValue = m[0][0]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if ( m[i][j] >maxValue ) { maxValue = m[i][j]; …
1
vote
1 answer

java cyclomatic complexity tools to use fully qualified class names

Most javancss tools use the method arguments as given in source code. The variable names are removed, but the class names are not FQN. For example, given following class. import java.io.File; public class Demo { public void show ( java.io.File…
Jayan
  • 18,003
  • 15
  • 89
  • 143
1
vote
4 answers

Existing library to calculate code complexity of a block of code

I'm given a string which contains an arbitrary amount of code. I want to calculate a number which represents the code complexity of that string. Something like: int complexity = Lib.FindComplexity(someString); I realize there are a lot of tools…
JoeB
  • 2,743
  • 6
  • 38
  • 51
1
vote
1 answer

Why is the Strategy design pattern applicable when reducing the cyclomatic complexity of the code?

The Strategy Design pattern, from the book Design patterns written by the Gang of Four, has the following statement in the Consequences section: Strategies eliminate conditional statements. The Strategy pattern offers an alternative to conditional…
1
vote
2 answers

Limiting a number without a conditional

Consider the following function: def absolute_value(some_number): if some_number < 0: return -some_number return some_number and this one: def absolute_value(some_number): return (some_number**2)**0.5 We can say that (in a…
1
vote
1 answer

Ways to Avoid if-else, switch-case in Factory design pattern

I am designing a validation module. It has 100 error codes(i.e. errcd_01, errcd_02,..,errcd_100) to be validated. In input I am getting a specific error code(i.e. errcd_01) out of above 100. Module should perform validation for that specific error…
1
vote
1 answer

Calculating Cyclomatic Complexity [Pseudocode]

The Cyclomatic Complexity of the pseudocode below is "4". Read A Read B IF A > 0 THEN IF B = 0 THEN Print “No values” ELSE Print B IF A > 21 THEN Print A ENDIF ENDIF ENDIF How do we count it? I heard that…
Bart
  • 13
  • 1
  • 4
1
vote
0 answers

How to use Ned Batchelder's script for McCabe's Complexity programmatically?

I'm trying to use Ned Batchelder's script for McCabe Complexity to get the complexity of codes written in Python 3, but I want to run it from a script instead of running it via command line. I've already installed the McCabe from pypi and the…
BloodySinner
  • 95
  • 1
  • 9
1
vote
1 answer

Can high cyclomatic complexity impact build time?

I know when we reduce cyclomatic complexity, it makes our code easier to read and maintain. But are there any other documented, tangible benefits, specifically around improving build time? It seems to me that, at least with the Microsoft CLR, that…
Kevin McDowell
  • 532
  • 6
  • 20