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

How to reduce 'complexity too high' with || - or operator

I've got a simple method that counts total lesson hours in the university schedule for additional modules in the department (students can attend many departments) def hours_total @hours_total = user.open_departments.each_with_object({}) do…
eudaimonia
  • 1,406
  • 1
  • 15
  • 28
3
votes
4 answers

How to refactor a long else if statement?

My code piece reached a cyclomatic limit, trying to think of a way to refactor. if(item.oldLabelType === 'Fruits') { item.newLabel = this._processFruitsLabel(item.oldLabel); } else if(item.oldLabelType === 'Vegetables') { item.newLabel =…
3
votes
0 answers

How do I export tslint static analyzer cyclomatic complexity report in vscode?

I want to export tslint cyclomatic complexity report in any format like json, csv, tsv etc. How can I do that ? Are there any third party libraries available in the market ? Currently I am using tslint extension in my vscode. Please help
Deepak
  • 1,510
  • 1
  • 14
  • 27
3
votes
1 answer

Why is the cyclomatic complexity of an event 2?

I'm scrutinizing cyclomatic complexity in an application with the purpose of testing anything that's got a cyclomatic complexity greater than 1. What I'm seeing is that a very simple event's add accessor has a cyclomatic complexity of 2. Why is…
Trevor
  • 4,620
  • 2
  • 28
  • 37
3
votes
0 answers

Restrict Jacoco coverage calculcation on methods with cyclomatic complexity > 2

Is there a way to tell Jacoco not to take into account the methods with very low cyclomatic complexity(CyC)(CyC<2) while calculating the coverage percentage? NB:I think this is doable in Atlassian-clover either by filtering out the method with CyC…
3
votes
2 answers

Is there a way to make PMD ruleset ignore Hashcode & Equals methods for Cyclomatic complexity?

I'm already know about the various annotation or comment I can add into my code, but it's not what I'm looking for. I'm looking for a way that only have me modify the CyclomaticComplexity rule. I don't seen any option in the rule, although it seems…
TheBakker
  • 2,852
  • 2
  • 28
  • 49
3
votes
1 answer

Use Automapper for mapping big domain models to database objects

I would like to use Automapper to map my model objects to database objects. Let say database object is over 30 fields and I want map 10 out of 20 properties from my model. To make it more complex I need to map different properties when I update…
Wojciech Markowski
  • 1,074
  • 1
  • 11
  • 15
3
votes
1 answer

How to draw a control flow graph for a nested for loop?

for(num2 = 0; num2 <= 3; num2++) { for(num1 = 0; num1 <= 2; num1++) { cout<< num2<< " " << num1<< endl; } } how to draw a control flow graph for the above code segment? Thanks in advance :)
3
votes
3 answers

Is there a limit to how many levels you can nest in JavaScript?

Say you have this really complex algorithm that requires dozens of for loops. Does JavaScript have a limit to how deep loops can be nested or is there no limit? What is the best practice for deep nested for loops? I tried searching on MDN but…
3
votes
1 answer

How can I reduce cyclomatic complexity from this piece of code?

I'm refactoring some legacy code. I get error from jshint about cyclomatic complexity which I'm trying to figure out how to fix the warning. The code is in node.js so anything in JavaScript is very much welcome. if (rawObj.title) { …
toy
  • 11,711
  • 24
  • 93
  • 176
3
votes
3 answers

Reduce number of for loops in method

I'm trying to reduce the number of for loops in this method, but I'm not sure how to do that while still keeping the logic in tact and not moving to another method. I want to do this for the sake of reducing the McCabe Cycolmatic Complexity, which…
Carlo Otto
  • 47
  • 1
  • 5
3
votes
0 answers

How to get the lines that were executed in Pharo Smalltalk?

I would like to get the lines that were executed in a really big execution path. I'm using Pharo 4.0 Is there a way to instrument this in an image without having to manually tag relevant methods? or maybe there is an alternative that will help to…
Ray Doyle
  • 829
  • 1
  • 6
  • 8
3
votes
1 answer

What exactly is the class complexity

Their wiki only says the obvious (average complexity of the class), but what does it actually mean? I know for method complexity, 15-20 is usually the upper bound for a testable and maintainable code.
Pietross
  • 313
  • 1
  • 3
  • 9
3
votes
1 answer

Cyclomatic Complexity for multiple return statements

I read about Cyclomatic complexity and multiple return statements, but I am a little bit confused, because of the different opinions for multiple return statements. First of all, during Cyclomatic Complexity calculation should I count each return…
Deniz
  • 858
  • 10
  • 31
3
votes
1 answer

custom threshold for CA1502 in visual studio 2013 ultimate

This question: Custom threshold for CA1502 discusses how to set up custom thresholds for code metrics rules in code analysis. I have the same problem, but think that the old question is out of date. To repeat: In particular, we would like our Build…
perfectionist
  • 4,256
  • 1
  • 23
  • 34