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

Guidelines for permissible maximum linearly-independent cyclomatic complexity?

For software engineering metrics, what are some guidelines on the maximum permissible linearly-independent cyclomatic complexity? For a properly designed module, what is the upper-bound on cyclomatic complexity?
Eugene
  • 10,957
  • 20
  • 69
  • 97
0
votes
2 answers

How to reduce cyclomatic complexity in for...if loop in c#

I have a simple code to print the rows and columns of a CSV file. This is my code and the cyclomatic complexity is coming out to be 6 for this class. I want to reduce it to as low as possible. class PrintCSV { /// /// Print all the…
0
votes
1 answer

Understanding McCabe's Cyclomatic complexity

McCabe's cyclomatic complexity measure is computed as follows: V = e - n + 2 where e = number of edges; n = number of nodes Why 2 is added to (e-n) in order to obtain the result?
0
votes
1 answer

Reduce cyclomatic complexity

I'm writing an NMEAParser library. As its name suggests, it parses NMEA sentences. Nothing crazy. Its entry point is a function that accepts an NMEA string as its only parameter and looks at its beginning to pass it to the right decoder. Here is the…
X99
  • 905
  • 10
  • 24
0
votes
1 answer

Creating and filling an object in two separate functions or in a single one?

I've encountered this problem many times. The problem is that I have an array of elements that consist of a key and a value and I need to reduce the array to an object with the keys and the sum of values for each key (e.g. ["foo", 1], ["bar", 1],…
asharpharp
  • 169
  • 1
  • 9
0
votes
1 answer

Can using an object with true and false keys or the logical operators instead of a single if else reduce cyclomatic complexity?

So I'm currently learning JavaScript and the lecturer who taught the course I'm currently watching constantly talks about reducing cyclomatic complexity. He uses a few tricks for doing so which I cannot understand. I think I understand why a switch…
0
votes
1 answer

What would happen if a control flow graph consists of multiple start and/or stop nodes when calculating Cyclomatic Complexity

I want to know how will it affect to the Cyclomatic Complexity when having multiple start or stop nodes in a control flow diagram.If you could explain the relationship between Cyclomatic Complexity and Start/Stop nodes it will be a great help.
0
votes
1 answer

Cyclomatic Complexity of multiple if statements

if (k = = 1) r + = a; else if (k = = 2) r + = b; else if (k = = 3) r + = c; else r + = d; switch (k) { case 1: r + = a; break; case 2: r + = b; break; case 3: …
George Silva
  • 385
  • 5
  • 13
0
votes
4 answers

Reducing cyclomatic complexity

I have a class with some 20+ fields of the same type that are populated during different stages of the object lifecycle. One of the class methods should return the field value based on the field name. So far I have something like this: public String…
Fenero
  • 11
0
votes
1 answer

How does radon calculate cycloMatic complexity of a class?

I ran radon cc -s myFile.py to calculate Cyclo metrics for it, I have pasted part of the result below ... (more stuff) C 37:0 MyClass - A (3) M 40:4 MyClass.letter - A (2) M 47:4 MyClass.fullname - A (2) M 58:4 MyClass.__str__ - A (1) ... (more…
95_96
  • 341
  • 2
  • 12
0
votes
0 answers

How to get low cyclomatic complexity with switch statements?

I have a function that takes an input and does a switch case based on a specific input property. So the function itself is pretty simple, as you can see below function handleMessage(message) { switch (message.type) { case "a": …
Jimi
  • 1,605
  • 1
  • 16
  • 33
0
votes
1 answer

Are there anyway to reduced complexiy of following java code?

I need to reduced following java method complexity according to sonar acceptable level. now ,it given this like sonar issue. need some expert help to do this. public List Y(final DateTime treatmentDiscontinueTime, …
uma
  • 1,477
  • 3
  • 32
  • 63
0
votes
0 answers

Are there any way reduced 'The Cyclomatic Complexity' of follwing java code.?

I am want to know following two code complexity can be reduced further. They are given complexity issues in my sonar server. need some experts help to resolve this issues without damage existing logic. I am using java 8 , it also fine are their any…
uma
  • 1,477
  • 3
  • 32
  • 63
0
votes
1 answer

How to set a custom complexity limits for sonar in java?

Is there a way to set complexity limits for sonar? I prefer very simple codes (at most one if or loop), but I cannot find a setting like sonar.cpd.java.minimumtokens or sonar.cpd.java.minimumLines exist for code duplications (and does not seem to…
0
votes
2 answers

How to lower cyclomatic complexity of switch with 11 cases

I have to check if a value is withing a range (0..9, 10..19..up to 100) and return a value depending on the range. The cyclomatic complexity of my function is 12 and I need to lower it to at least 9. I'm really at a loss here. I wanted to use an…