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

Are there any good inter-method / inter-class complexity metrics?

A common advice on method/function length is to write small methods to increase both readability and testability. These are measured, for example, by cyclomatic and cognitive complexity metrics. Similar metrics apply to classes, or in general,…
0
votes
0 answers

Cyclomatic Complexity with multiple & different returns

Working on validating a tool calculating Cyclomatic Complexity and have had a bit of issue with trying to figure out how it works with multiple returns that return different variables. I've come across articles that handle multiple returns but have…
Emonnier
  • 41
  • 5
0
votes
2 answers

Reduce the cyclomatic complexity of a typescript method

Provided is the typescript function, where we need to reduce the cyclometric complexity. I am thinking off to provide the inverted if but that doesn't help much. updateSort(s: Sort) { if (s.active !== this.personSort.active || s.direction !==…
0
votes
1 answer

Calculate cyclomatic complexity from file in different programming language via script

I am working on writing a script that outputs information about my written source code. So far, I am analysing the code with Python scripts that I have written. There is a Test.cs class which is used for this analysis. Now I want to calculate the…
41 72 6c
  • 1,600
  • 5
  • 19
  • 30
0
votes
3 answers

multiple arguments and conditions in a function in javascript

I have a function that return some informations about employees from an object database I'm facing some complexity ESlint Problems, so i need to find a way to minimize it or find a smart way to do this instead of using a whole set of if…
Rafo
  • 511
  • 3
  • 17
0
votes
0 answers

What causes high cyclomatic complexity in practice?

I am trying to understand Cyclomatic complexity - mainly to get my head around if it's useful. I did a quick check of my codebase and all the functions had a complexity < 6. This led me to try and write a "highly" complex function, but it seems very…
MYK
  • 1,988
  • 7
  • 30
0
votes
3 answers

Reducing Cyclomatic Complexity in a select

Good day coding godz, I am trying to clean up some code and was wondering how to reduce the Cylcomatic Complexity on a method I have created. This method is used during an Import of a CSV file. One of the fields in the CSV file is a license type…
Gary
  • 21
  • 6
0
votes
0 answers

Calculate the Cyclomatic Complexity

Calculate the CC (Cyclomatic Complexity) of the following code snippets. I am not sure if I have drawn the Control Flow Graph correctly and furthermore if my calculation for the Cyclomatic Complexity is correct. And as for the formula M = E - N +…
Mr. Hankey
  • 954
  • 1
  • 5
  • 12
0
votes
0 answers

Optimizing methods with multiple if checks on getter values

I have a method where I fetch user input, check if certain values exist, and based on that build my own custom input object that I would use to search in a database. The code for the search method is as follows. public SearchDocumentResult…
AnOldSoul
  • 4,017
  • 12
  • 57
  • 118
0
votes
1 answer

cyclomatic complexity of entire project solution.by not using directly any software

Calculate Cyclomatic Complexity using c# code. I am developing an application, that will take solution name from user and will show cyclomatic complexity of that solution in c#. please help me. I am taking solution name from the user and i have to…
Priya Rani
  • 17
  • 1
  • 6
0
votes
2 answers

Reduce complexity of counting neighbours (Conway's Game of Life)

I have to implement Conway's Game of Life. Everything works as it should and given tests are passing. My only problem is that this method gives complexity error while running PMD rules on my file. I understand that so many if sentences are the cause…
Meowz
  • 13
  • 2
0
votes
2 answers

High complexity preventing function from

Firstly, because the code I am using is big so here is the link to the code. I have this function that runs other functions if a specific input is given: allowed_commands = ['help', 'joblist', 'job', 'jobresign', 'work', 'bal', 'balance', 'dep',…
0
votes
1 answer

Rubocop cyclomatic complexity of top-level code

Is there a reason why rubocop excludes top-level code when evaluating cyclomatic complexity? It only returns complexity for code independent paths through a method. Is there a way to include top-level code in this analysis?
user8710949
  • 17
  • 1
  • 4
0
votes
2 answers

How to refactor to reduce complexity of this code?

I have the following code and i need to refactor it to reduce complexity and increase modularity and encapsulation. I also need to reduce the ck metrics value. private void initialiseVehicle(String vehicleName) { …
Mayabi
  • 1
  • 2
0
votes
2 answers

Design pattern to reduce cyclomatic complexity

I have to send invoices to cutomers through the related customer's API. Each customer chooses how to be contacted (email, fax, sms...). My problem is that each time I add a new "contact type" I increase the cyclomatic complexity of my script. Here…
Shaolin
  • 415
  • 5
  • 11