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.

271 questions
100
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
  • 38,558
  • 14
  • 69
  • 90
75
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
  • 167,397
  • 108
  • 388
  • 523
71
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,042,979
  • 435
  • 3,649
  • 4,283
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…
Paul Robinson
  • 6,790
  • 3
  • 33
  • 36
56
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,348
  • 1
  • 10
  • 18
47
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,505
  • 1
  • 12
  • 20
41
votes
1 answer

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
38
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
  • 10,603
  • 16
  • 52
  • 73
30
votes
12 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,351
  • 7
  • 59
  • 86
30
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…
Herter
  • 8,873
  • 9
  • 36
  • 45
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
  • 5,440
  • 14
  • 53
  • 88
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
  • 163,579
  • 332
  • 834
  • 1,328
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,741
  • 3
  • 20
  • 35
18
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
  • 8,104
  • 16
  • 74
  • 120
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
  • 107,354
  • 40
  • 225
  • 343
1
2 3
18 19