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

Python Refactor this function to reduce its Cognitive Complexity from 20 to the 15 allowed

In Vscode, I see this message from sonarlint and trying to figure out how to reduce the Cognitive Complexity of this function. Any assistance is appreciated in advance. restapi.py(86, 5): +1 restapi.py(86, 25): +1 restapi.py(89, 9): +2 (incl 1 for…
Walkn
  • 13
  • 5
1
vote
0 answers

Impact of '??' on cyclomatic complexity in Dart

Should the ??, ??=, ?., ?... operators consider as new path in the code when calculating the cyclomatic complexity of a method?
nipunasudha
  • 2,427
  • 2
  • 21
  • 46
1
vote
1 answer

In Delphi, should I care about Cyclomatic Complexity for "case of" statements?

I'm using the built-in "Method Toxicity Metrics" in Delphi 10.3 in my code review flow and I have the following method: function LoadTagsFromFile(const Filename: String; var Tags: TFileTags; var LastError: Integer): Boolean; var AudioType:…
AlexV
  • 22,658
  • 18
  • 85
  • 122
1
vote
1 answer

Cyclomatic complexity of Scratch programs

I'm writing a paper on introductory programming for young learners. To determine the difficulty level of a program, I'd like to draw on the concept of cyclomatic complexity, which I've come across during my research. I'd really appreciate if you…
pretzel
  • 31
  • 1
1
vote
3 answers

reduce complexity of if elseif conditions

I have one function which is having if elseif conditions and the cyclomatic complexity is approaching 5. How do I reduce it? function testFunc() { var step = getModel('step'); if(step === 1) { this.resetTask(); //calling some…
Liz.
  • 795
  • 2
  • 13
  • 31
1
vote
1 answer

What will happen if we have multiple start and stop in cyclometric graph

recently i had a question that was so confusing.What would happen if a control flow graph consists of multiple start and/or stop nodes?
mithelan
  • 190
  • 9
1
vote
0 answers

Cyclomatic Complexity - Control Flow Graph

I am a newbie. Could you please explain what would happen if a control flow graph consists of multiple start and/or stop nodes? I searched for the answer but was not able to find a proper one Thank You in advance
Prasadika
  • 897
  • 2
  • 20
  • 36
1
vote
1 answer

Turn on cyclmatic complexity in JSHint

I am using JSHint and I want to turn on cyclomatic complexity. From JsHint homepage and JsHint Docs , JsHint seem to be able to measure it. But after I ran this command jshint test.js to test the below file //test.js $(document).ready(function() { …
BombShelley
  • 107
  • 9
1
vote
0 answers

Cyclomatic Complexity Calculation on Presenter Methods and Callbacks

everyone. I need help in determining the cyclomatic complexity regarding this tested method. @Override public void renderData() { mRepository.render(new TvShowsRepositoryCallback() { @Override public void onRenderingData() { …
1
vote
1 answer

Cyclomatic Complexity OF Model Class

Can anybody help me to understand why my model class has cyclomatic complexity of 89. When I run PMD it gives me 'The class 'GetResponse' has a total cyclomatic complexity of 89 (highest 1)'. Please find code snippet as: public class GetResponse …
1
vote
1 answer

How to reduce the cyclomatic complexity of my code?

So I am trying to make a chatbot in python. An online ide(repl.it) is what I use to run the code from anywhere. It has a cyclomatic complexity limit of 15. I would want to make something really complex without going over the limit, Is this…
Axisnix
  • 2,822
  • 5
  • 19
  • 41
1
vote
1 answer

Use PyLint to calculate Cyclomatic Complexity

I want to calculate Cyclomatic Complexity by using PyLint. I need to have a python script that calculates the complexity of many modules at once. I have tried using command as well as through python program. This is the command i'm using: pylint…
1
vote
1 answer

Geenrate Method Call from string PHP

I have Cyclomatic Complexity issue with a switch case statement, so I need to refactor switch/case block of code. I trying to create an array with keys and as value to set method call i.e…
Stevan Tosic
  • 6,561
  • 10
  • 55
  • 110
1
vote
4 answers

How to reduce if/else complexity?

I have some if/else statements nested but I want to reduce their overhead. In the example, I'm evaluating from which dropdown has an li item been clicked, and if this li item is the first (currentIndex === 0). The code: if (parentIndex === 1) { //…
1
vote
1 answer

cyclomatic complexity with MetrixReloaded

Im trying to analyse the cyclomatic complexity of a codebase using the MetricsReloaded plugin in Android Studio (should be the same as IntelliJ). I have it working and get the output but am unsure on what the output actually means. The output I see…
cowley05
  • 123
  • 1
  • 1
  • 10