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

Fix cyclomatic complexity if/else inside loop

If I have a loop with a condition inside like this, where I need an internal and an external variable: let b = get(b); for(let a in myArray){ if(a==b){ // Do something }else{ // Do another thing } } How can be the refector in…
0
votes
2 answers

Avoid multiple IF to ensure Mccabe complexity

I want to ask you your different points of view for the following scenario: imagine that we have several lists and something should be executed for those that are not empty: if l1 or l2 or l3 or l4 or l5 or l6 or l7 or l8 or l9: print 'we have…
Solar
  • 445
  • 1
  • 4
  • 12
0
votes
2 answers

How to enhance C.R.A.P. index for a switch-like function?

I have a very typical switch-like function that returns a clasiffication for a given input value (Body Mass Index in this case). (I'm working with this function, but it could be any other of the same nature) For now, it's pretty much like so: //…
alariva
  • 2,051
  • 1
  • 22
  • 37
0
votes
0 answers

Reducing the cyclomatic complexity with getters and setters, from multiples POJOS

this question is for Spring java. I have a problem with the bunches of If statements: Actually I have two pojos in a service layer. I get the field from one pojo. If this field contains info, I'll set the response to the other pojom passing…
0
votes
2 answers

Reducing if/else statement complexity?

I have a concise if/else statement below: function () { if (elem.attr('data-src-1') === '' && elem.attr('data-src-2') === '') { // scenario a } else if (elem.attr('data-src-1') === '' && elem.attr('data-src-2') !== '') { //…
0
votes
1 answer

Why complexity function was removed in Sonar 6.7?

In the release note: https://jira.sonarsource.com/jira/secure/ReleaseNote.jspa?projectId=10930&version=13972 ...show it as "Clean up complexity metrics" ...at Jira, just removed it but don't explain…
Omar F.
  • 27
  • 5
0
votes
1 answer

How do I reduce the McCabe Cyclomatic Complexity of this code

I have the java below. This is called from a menu, where the user enters a number (opcaoMenu) corresponding to an option, and it call a method based on this option. This code has cyclomatic complexity of 8, and I want to reduce it, but I have no…
0
votes
0 answers

Apache POI and cyclomatic complexity of code

I have an Excel file, which I am reading using Apache POI APIs; which gives me control over cells/columns in my Excel. I have around 17 columns in my Excel and these are with different datatypes. I want to populate a model object from these column…
Akshay Lokur
  • 6,680
  • 13
  • 43
  • 62
0
votes
0 answers

Method with cyclomatic complexity of 30 just has one code path

I have a method that creates a dictionary of delegates (below). As you can see, this method just has one linearly independent code path because it's literally just creating the delegates and putting them in the dictionary. It never executes any of…
Pedro Gordo
  • 1,825
  • 3
  • 21
  • 45
0
votes
1 answer

How to add cyclomatic complexity to sonar-cxx?

I'm trying to add lizard report (https://github.com/terryyin/lizard) to sonar in order to create issues over functions are over a threshold There are any way to achieve that?
Joan Esteban
  • 1,006
  • 12
  • 23
0
votes
2 answers

Measuring Cyclomatic Complexity

The code below takes in an array of package names and dependencies in the format {Package1: Dependency, Package2: Dependency} and puts them in a hashmap to be returned. According to my interviewer, the method has a cyclomatic complexity of 12, which…
Robert Lu
  • 441
  • 2
  • 7
  • 18
0
votes
2 answers

How to reduce Cyclomatic Complexity by refactoring the code?

The question is repeated one but still I am asking because by using the method suggested in the solution I am not able to reduce complexity significantly. The functions complexity is 28 and I have to reduce it below 10. private void…
Ankit Joshi
  • 261
  • 2
  • 11
0
votes
1 answer

Refactor a switch statement with cyclomatic complexity upper than 10

i've this code: for notif in EnvironmentManager.notif { if let type = notif.type { switch type { case .SitterAvailable: self.manageNotif(notif, title: "Une sitter disponible", storyboardName:…
Oleg G.
  • 550
  • 5
  • 25
0
votes
1 answer

C Code complexity metrics for the whole code base

I'm using Mccabe code complexity as my metric to evaluate my code base, however it only gives the code complexity score of each individual function. The overall code complexity score is given by the sum of scores of all functions in my code base.…
yangjeep
  • 9
  • 1
0
votes
1 answer

What are case-labeled statements and case labels?

Having read McCabe's article about cyclomatic complexity, it reads: A less frequently occurring issue that has greater impact on complexity is the distinction between “case-labeled statements” and “case labels.” When several case labels apply…