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
101
votes
8 answers

How can I analyze Python code to identify problematic areas?

I have a large source repository split across multiple projects. I would like to produce a report about the health of the source code, identifying problem areas that need to be addressed. Specifically, I'd like to call out routines with a high…
Jerub
  • 41,746
  • 15
  • 73
  • 90
77
votes
15 answers

What is Cyclomatic Complexity?

A term that I see every now and then is "Cyclomatic Complexity". Here on SO I saw some Questions about "how to calculate the CC of Language X" or "How do I do Y with the minimum amount of CC", but I'm not sure I really understand what it is. On the…
Michael Stum
  • 177,530
  • 117
  • 400
  • 535
76
votes
12 answers

Conditional logging with minimal cyclomatic complexity

After reading "What’s your/a good limit for cyclomatic complexity?", I realize many of my colleagues were quite annoyed with this new QA policy on our project: no more 10 cyclomatic complexity per function. Meaning: no more than 10 'if', 'else',…
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
65
votes
4 answers

In a switch vs dictionary for a value of Func, which is faster and why?

Suppose there is the following code: private static int DoSwitch(string arg) { switch (arg) { case "a": return 0; case "b": return 1; case "c": return 2; case "d": return 3; } return -1; } private…
cubetwo1729
  • 1,438
  • 1
  • 11
  • 18
56
votes
15 answers

Do you find cyclomatic complexity a useful measure?

I've been playing around with measuring the cyclomatic complexity of a big code base. Cyclomatic complexity is the number of linearly independent paths through a program's source code and there are lots of free tools for your language of choice. The…
elasticrat
  • 7,060
  • 5
  • 36
  • 36
55
votes
7 answers

Calculate Cyclomatic Complexity for Javascript

Are there any tools available for calculating Cyclomatic Complexity in Javascript? I've found it a very helpful metric in the past while working on server side code, and would like to be able to use it for the client side Javascript I write.
Karl
  • 1,615
  • 1
  • 14
  • 20
42
votes
2 answers

Code Metrics Calculation in Visual Studio

What is the prefered score range for the code metrics calculation for the following Maintainability Index Cyclomatic Complexity Depth of Inheritance class Coupling
39
votes
28 answers

How complex should code be?

I'm studying about algorithms which can help me write smaller but more complex code. Instead of writing 150 lines of if-else statements, I can design an algorithm that does it in 20 lines. The problem is a lot of these algorithms can be complex…
danmine
  • 11,325
  • 17
  • 55
  • 75
34
votes
3 answers

How to reduce cyclomatic complexity?

I'm working on a class which sends a RequestDTO to a Web Service. I need to validate the request before it is sent. The request can be sent from 3 different places and there are different validation rules for each "requesttype", e.g. request1 must…
Linora
  • 10,418
  • 9
  • 38
  • 49
31
votes
13 answers

Measuring the complexity of SQL statements

The complexity of methods in most programming languages can be measured in cyclomatic complexity with static source code analyzers. Is there a similar metric for measuring the complexity of a SQL query? It is simple enough to measure the time it…
epotter
  • 7,631
  • 7
  • 63
  • 88
23
votes
4 answers

Calculation of Cyclomatic Complexity

I am at learning stage of cyclomatic complexity(CC). For practise, I am calculating cyclomatic complexity of 2 examples and want to confirm if my answers are correct or not... Referring to wikipedia, CC is given by M = E − N + 2P where: E = the…
tech_human
  • 6,592
  • 16
  • 65
  • 107
23
votes
5 answers

Best tool to determine code Cyclomatic complexity

Can people suggest the best tool to determine the cyclic complexity with in a C# winforms code base.
leora
  • 188,729
  • 360
  • 878
  • 1,366
21
votes
5 answers

how could I reduce the cyclomatic complexity?

Whenever I lint a piece of code I'm working on I get the This function's cyclomatic complexity is too high. (7). But I'm a bit confused on how I could rewrite it in such way so it works. This would be the function that keeps throwing that…
Roland
  • 9,321
  • 17
  • 79
  • 135
20
votes
8 answers

Seeking clarifications about structuring code to reduce cyclomatic complexity

Recently our company has started measuring the cyclomatic complexity (CC) of the functions in our code on a weekly basis, and reporting which functions have improved or worsened. So we have started paying a lot more attention to the CC of…
RickL
  • 2,811
  • 3
  • 22
  • 35
18
votes
4 answers

What is the highest Cyclomatic Complexity of any function you maintain? And how would you go about refactoring it?

I was doing a little exploring of a legacy system I maintain, with NDepend (great tool check it out), the other day. My findings almost made me spray a mouthful of coffee all over my screen. The top 3 functions in this system ranked by descending…
JohannesH
  • 6,430
  • 5
  • 37
  • 71
1
2 3
19 20