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

Cyclomatic complexity using jar files

I am working on a project which requires me to find the cyclomatic complexity of Apache ant (Versions 1.1 through 1.6). I have been asked to use the jar files for this purpose. I used a couple of tools (Xdepend trial version and Cyvis ) to have a…
Aman Neelappa
  • 145
  • 1
  • 1
  • 9
0
votes
3 answers

Need help reducing cyclomatic complexity

I've got a method called UpdateUserDevices (UserModel). The UserModel contains a List which associates a list of devices with a specific user. (One-to-many). When I call the method, everything is working as excpected, however it's quite…
Chase Florell
  • 46,378
  • 57
  • 186
  • 376
0
votes
2 answers

Best way to check that at least one textbox of many has content?

I have a 'search page' where it is required that at least one textbox has some input. The following method verifies this like so: if (!String.IsNullOrEmpty(txtNome.Text)) { return true; } if…
0
votes
2 answers

cyclomatic complexity for ActionScript 3

i am trying to calculate the cyclomatic complexity of my software but i am a bit confused. From what i understand it is the amounts of paths that are needed to be tested to cover the whole software. Usually there are if statements and loops which…
0
votes
1 answer

Cyclomatic complexity (Vg) - minimal or exact number of minimum of independent paths?

I thought that Vg=minimal number of independent paths but today I saw a presentation of one of my teachers and it said: Vg is equal or greater than number of independent paths. It is true? I was sure that when counting cyclomatix complexity, it is…
John V
  • 4,855
  • 15
  • 39
  • 63
0
votes
1 answer

Facade/Service architecture

Before I ask my question I must describe how our applications are built. We run several web applications that uses ejb´s in the service layer. I try to describe the communication with a short example: A JSF bean (PersonHandler) calls a facade to…
user652158
0
votes
1 answer

How can I check the cyclomatic complexity of an Xcode project's source?

I need to analyse a large Xcode project for its cyclomatic complexity. Has anyone got an easy way of checking the cyclomatic complexity of their code contained within an Xcode project? Is there a tool that is setup to read xcode projects, perhaps? I…
Stretch
  • 3,669
  • 2
  • 28
  • 40
0
votes
1 answer

Control flow graph & cyclomatic complexity

I have to find the control flow graph and cyclomatic complexity for this code and then suggest some white box test cases and black box test cases. But I am having trouble making a CFG for the code. Would appreciate some help on test cases as…
0
votes
1 answer

Approach to reduce Cyclometic Complexity

I work on a big java project(mobile application), and got the "thankful" job to review and optimize/refactore code, because of the poor performance(high complexity). Note: I'm totally new to Java(my background is C/C++), therefore I applogize for…
arge
  • 635
  • 1
  • 7
  • 16
0
votes
2 answers

How do you list the most complex methods of a project in Sonar?

In Sonar, you can have the list of classes sorted by average complexity of methods of the class (when you list the complexity per method from the dashboard) But how do you list the most complex methods project-wide ?
Jérôme Radix
  • 10,285
  • 4
  • 34
  • 40
-1
votes
1 answer

Calculating Cyclometric Complexity?

How do you properly calculate Cyclmetric Complexity? According to Wikipedia the cyclometric complexity of the following code: if (c1()) f1(); else f2(); if (c2()) f3(); else f4(); is 3. But I understand it as 4: 2 * 2 = 4 Looking…
-1
votes
5 answers

How to reduce the complexity of this if else statements in Javascript?

I have the below scenario which has multiple if else conditions. The cyclomatic complexity of the below code is showing as 7. Is there a better way to write the below code snippet using Javascript which reduces the complexity of the code ? …
Vivek
  • 281
  • 2
  • 7
  • 21
-1
votes
1 answer

Cyclomatic complexity of C# Linq?

As per Wiki: Cyclomatic complexity is a software metric, used to indicate the complexity of a program. It is a quantitative measure of the number of linearly independent paths through a program's source code. It was developed by Thomas J.…
Ruslan
  • 9,927
  • 15
  • 55
  • 89
-1
votes
1 answer

Finding ? operator in AST

I'm doing some exercise in Rascal. When I try to determine the Cyclomatic complexity of a Java method getting methods from an AST. I would like to evaluate the ? operator. As it is not determined by '/if(_, _, )', I tried to determine it using…
Ben
  • 1
  • 1
-1
votes
1 answer

Whitebox testing Cyclomatic complexity

I'm trying to learn the cyclomatic complexity, but i'm stuck in this step. How does it gets the sequence value <1, 13, 18, 45, 49> as highlighted below? https://i.stack.imgur.com/UFTyU.jpg
1 2 3
19
20