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

How do you calculate cyclomatic complexity for R functions?

Cyclomatic complexity measures how many possible branches can be taken through a function. Is there an existing function/tool to calculate it for R functions? If not, suggestions are appreciated for the best way to write one. A cheap start towards…
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
17
votes
3 answers

Cyclomatic Complexity of switch case statement

I'm confusing about the CC of switch statement If I have following code: if (n >= 0) { switch(n) { case 0: case 1: printf("zero or one\n"); break; case 2: printf("two\n"); …
CodinCat
  • 15,530
  • 5
  • 49
  • 60
16
votes
5 answers

How can I reduce the Cyclomatic Complexity of this?

I have a method that receives an Object and does something based on what type of object it detects: void receive(Object object) { if (object instanceof ObjectTypeA) { doSomethingA(); } else { if (object instanceof…
Nathan
  • 5,322
  • 5
  • 24
  • 24
16
votes
2 answers

cyclomatic complexity = 1 + #if statements?

I found the following paragraph regarding cyclomatic complexity on Wikipedia: It can be shown that the cyclomatic complexity of any structured program with only one entrance point and one exit point is equal to the number of decision points (i.e.,…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
15
votes
3 answers

How to check code cyclomatic complexity in Android studio?

How to check code cyclomatic complexity in Android Studio? Is there any plugin available for Android Studio to check code complexity?
vishalk
  • 659
  • 1
  • 4
  • 12
14
votes
1 answer

Tool to calculate Cyclomatic Complexity for C code

I want a tool to analyse my code quality and after a bit of googling I think I understand that Cyclomatic Complexity could be something to start with here. I have tried to get several tools working but without success (found out that most are…
Olppah
  • 790
  • 1
  • 10
  • 21
14
votes
4 answers

Turn off cyclmatic complexity in JSHint

I am using JSHint and I want to turn off cyclomatic complexity. How can I do this?
user2950593
  • 9,233
  • 15
  • 67
  • 131
13
votes
2 answers

Are there any tools for visualizing code complexity or graphing method calls in Objective-C?

I'm hoping to show a visualization of the code base which can show areas that are overly complex and intertwined. I know what clang is, but I'm not sure it gives me what I want in this case.
zekel
  • 9,227
  • 10
  • 65
  • 96
13
votes
5 answers

Automatic generation of Unit test cases for .NET and Java

Is there a good tool to generate unit test cases given say a .NET or Java project, it generates unit test cases that would cover an almost 100% code coverage. The number of test cases could be directly proportional to the cyclomatic complexity of…
yoitsfrancis
  • 4,278
  • 14
  • 44
  • 73
13
votes
4 answers

How to improve Cyclomatic Complexity?

Cyclomatic Complexity will be high for methods with a high number of decision statements including if/while/for statements. So how do we improve on it? I am handling a big project where I am supposed to reduced the CC for methods that have CC > 10.…
yeeen
  • 4,911
  • 11
  • 52
  • 73
13
votes
7 answers

Reduce Cyclomatic Complexity of Switch Statement - Sonar

I want to reduce cyclomatic complexity of my switch case my code is : public String getCalenderName() { switch (type) { case COUNTRY: return country == null ? name : country.getName() + HOLIDAY_CALENDAR; case CCP: …
Amar Magar
  • 840
  • 1
  • 11
  • 15
12
votes
4 answers

Cyclomatic complexity metric practices for Python

I have a relatively large Python project that I work on, and we don't have any cyclomatic complexity tools as a part of our automated test and deployment process. How important are cyclomatic complexity tools in Python? Do you or your project use…
Matt Messersmith
  • 12,939
  • 6
  • 51
  • 52
12
votes
1 answer

Cyclomatic Complexity and variants

Whats the diferrence between essential, design complexity, extended cyclomatic complexity, cyclomatic complexity? I'm checking this metrics using a IntelliJ IDEA plugin.
Kennedy Oliveira
  • 2,141
  • 2
  • 20
  • 25
12
votes
2 answers

How to find Cyclomatic Complexity of Xcode Project?

I want to know is there any way to find the cyclomatic complexity of the project created in Xcode. Thanks
Baddu
  • 290
  • 2
  • 11
11
votes
3 answers

Control flow graph & cyclomatic complexity for following procedure

insertion_procedure (int a[], int p [], int N) { int i,j,k; for (i=0; i<=N; i++) p[i] = i; for (i=2; i<=N; i++) { k = p[i]; j = 1; while (a[p[j-1]] > a[k]) {p[j] = p[j-1]; j--} p[j] = k; } } I…
AJ.
  • 2,561
  • 9
  • 46
  • 81
1
2
3
19 20