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

Sonar -LOC & Cyclomatic complexity

How does Sonar calculates software metrics particularly LOC and cyclomatic complexity? Does it uses any particular tools? IF yes, please also give the names.
1
vote
2 answers

cyclomatic complexity of the pseudo-code

Cyclomatic complexity is number of test cases that are necessary to achieve thorough test coverage of a particular module. Consider the following pseudo-code : If (A > B) and (C > D) then A = A + 1 B = B + 1 Endif I think only two test…
1
vote
3 answers

How to reduce cyclomatic complexity inside an onClick() method

Let's imagine a simple construction: public void onClick(View view) { switch (view.getId()) { case R.id.btn_first: { // some code } break; case R.id.btn_second: { // some code …
Egor
  • 39,695
  • 10
  • 113
  • 130
1
vote
0 answers

How Improve c# control flow complexity automatically?

I search about an automatic tool to refactor & rearrange c# code blocks to improve control flow complexity. I have a messy C# code which uses "GOTO" and "switch" instead of "while & for" loops: public double GetMaximumRange(long startPoint,…
Jake Terro
  • 11
  • 2
1
vote
1 answer

How can I achive code metrics for C# code in Jenkins?

How can I achieve code metrics for C# code in Jenkins, for example Cyclomatic complexity?
0
votes
1 answer

Where to get N'SIQ for C#?

The latest official release of N'SIQ Code Metric Collector (2.1.4 built in 2010-03-31) doesn't support C#. But there are evidences in the internet that someone is using the NSIQ Collector Plugin for Hudson and Jenkins with unsupported languages like…
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
0
votes
0 answers

Build flag added to the configuration and causes pipeline fail

I am using STM32CubeIDE which is based on eclipse. The problem is that a flag is added and don't know how it was added it is called -fcyclomatic-complexity. It causes the inclusion of .cyclo files. How can I remove this build flag? or should it be…
BSaleh
  • 13
  • 4
0
votes
0 answers

Cyclomatic Complexity of the following Code

I would like to calculate the cyclomatic complexity of the code given below. However, I have problem while drawing its CGF (control flow graph). How to draw the code's CGF? 1: int a=10; 2: for (int i = 0; i < 10; i ++) 3: { 4: a++; 5: } 6: print a;
0
votes
0 answers

Is it possible to get a different result for calculating Cyclomatic complexity using 2 different methods?

I am trying to calculate the Cyclomatic complexity for this code. double power(int x,int y){ int exp; double res; if (y>0) exp = y; else exp = -y; res=1; while (exp!=0){ res *= x; exp -= 1; } if (y<=0) if(x==0) abort; else return…
0
votes
0 answers

Simple spell checker using Levenshtein distance

I have to implement a simple spell checker. Basically I have to user input an incorrect sentence or a word, then a number N, and then N correct words each on new line. The program has to output "incorrect word: suggestion". If there is no…
0
votes
0 answers

Control Flow Graph & Cyclomatic Complexity for the following code

I have to find the CFG and complexity for this code, but I'm kind of confused by conflicting results. Would appreciate some opinions and help if possible. Here's the code snippet: public function checkCredentials() { // 0 if (username.length < 5 ||…
VenoM
  • 453
  • 3
  • 9
  • 17
0
votes
1 answer

what will be the cyclomatic complexity of this c++ code

int x = 99; int a = 4; int parity; while (x < 100) { if (a % 2 == 0) { parity = 0; } else { parity = 1; } switch (parity) { case 0: cout << "even "; case 1: cout << " odd "; …
0
votes
1 answer

Are there any tools that are able to do cyclomatics on Pro*C++ sources?

Are there any tools that are able to do code metrics on Pro*C++ sources? I haven't been able to find anything specific via Google. Does anyone have any experience with this?
EvilTeach
  • 28,120
  • 21
  • 85
  • 141
0
votes
0 answers

What is the cyclomatic complexity of this code?

For the code down below, SonarQube and ESLint calculate a cyclomatic complexity of 11. I calculated that too. However, the IntelliJ Linter calculates a cyclomatic complexity of 12. Is the IntelliJ Linter incorrect or exists there another way of…
0
votes
1 answer

Cyclomatic Complexity in Intellij

I was working on an assignment today that basically asked us to write a Java program that checks if HTML syntax is valid in a text file. Pretty simple assignment, I did it very quickly, but in doing it so quickly I made it very convoluted (lots of…