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
5
votes
3 answers

What exactly is the token count in functions/methods used for?

I've been using some tools to measure code quality and CCN (Cyclomatic Complexity Number) and some of those tools provides a count for tokens in functions what does that count says about my function or method? What is it used for?
Black Sheep
  • 1,665
  • 1
  • 22
  • 32
5
votes
3 answers

Why is the cylcomatic complexity of this function 12?

I have a (C#) function that checks four sets of conditions and returns a bool. If any of them are true, it returns true. I'm sure I could simplify the logic but I want it to be fairly readable. The CodeMaid extension in Visual Studios and tells me…
Charles Clayton
  • 17,005
  • 11
  • 87
  • 120
5
votes
2 answers

Why does this code have high cyclomatic complexity - or is it a bug in PHPMD in Jenkins?

I'm trying to understand how Cyclomatic Complexity works and how I can avoid the warnings. Yes, I understand that the goal of writing code is not to avoid arbitrary warnings, but I'd at least like to know what's going on so I can decide if the code…
matt
  • 1,947
  • 1
  • 19
  • 29
5
votes
4 answers

How to calculate cyclomatic complexity of a project (not a class/function)?

How to calculate cyclomatic complexity of the entire Java project? I have complexities for every method, but how to aggregate them all into one numeric metric? Any ideas or existing methods? I'm not looking for a tool, but for an algorithm. Simple…
yegor256
  • 102,010
  • 123
  • 446
  • 597
4
votes
0 answers

Should I use cyclomatic complexity in React?

I'm working on a React application using functional components. The lint configuration file has Cyclomatic Complexity enabled. That means that every component or custom hook that has too many logical conditions is marked with an error. Cyclomatic…
4
votes
1 answer

Why cyclomatic complexity of code with `switch` higher than that of with 'if-else'

I have two sample functions TestComplexityIf and TestComplexitySwitch. VisualStudio-2017 'Calculate Code Metrics' tool reports a cyclomatic complexity of 10 for the function with switch statement ad 7 for the one with if-else. I wonder how the…
FaisalM
  • 724
  • 5
  • 18
4
votes
1 answer

How to calculate cyclomatic complexity of a Clojure function?

What would be a reasonable way to calculate the cyclomatic complexity of a Clojure function? It's easy to count decision points based on functions like 'if' and 'cond', but it starts to get tricky with macros. Anyone has tried this for Clojure or…
Maurits Rijk
  • 9,789
  • 2
  • 36
  • 53
4
votes
1 answer

Cyclomatic complexity difference between switch/cases and in_array

Scenario I need to check if my $type_id variable is one of a certain set of IDs. For no reason other than readability, I went with switch($type_id) { case Type::SOME_TYPE: case Type::SOME_OTHER_TYPE: ... //do stuff where most of…
Alec
  • 1,986
  • 4
  • 23
  • 47
4
votes
2 answers

Obtaining Cyclomatic Complexity

I am looking into calculating the cyclomatic complexity of java methods using Rascal. One approach to this would be: getting the AST from the method use visit pattern on this tree check for the following keywords which all increase the CC with one:…
Lorenzo.A
  • 67
  • 4
4
votes
2 answers

Null Check's increasing cyclomatic complexity

I have an java POJO class as below. public class EmployeeVo { private String firstName; private String middleName; private String lastName; private String suffix; private String addressline1; private…
Nayeem
  • 681
  • 15
  • 35
4
votes
0 answers

An error occurred while calculating code metrics for target file

When i do calculate code metrics to my project, i am getting this error below. How can i solve it? Project: AA.Tests.Repository Configuration: Debug Scope: None Assembly: G:\WS\selin\src\Binaries\Win32\bin\AA.Tests.Repository.dll Maintainability…
cell-in
  • 709
  • 2
  • 11
  • 27
4
votes
6 answers

Reducing the cyclomatic complexity, multiple if statements

I have the following code: private Facility updateFacility(Facility newFacility, Facility oldFacility) { if (newFacility.getCity() != null) oldFacility.setCity(newFacility.getCity()); if (newFacility.getContactEmail() != null) …
Akeshwar Jha
  • 4,516
  • 8
  • 52
  • 91
4
votes
3 answers

How do I find cycles in my object hierarchy?

There is a class Company, which has reference to another instance of Company to represent the parent. Lets say there are four companies c1, c2, c3 & c4 and c2, c3, c4 has parent company set as c1. For example: public class Company { public Company…
Mukesh Kumar
  • 945
  • 4
  • 11
  • 26
4
votes
1 answer

Cyclomatic Complexity: Resharper vs CodeMaid

I've been using both CodeMaid and Resharper for some time now, and noticed they both calculate Cyclomatic Complexity. Although, their calculation always differ, so I'm thinking that their calculating logic also does. But, to your experience, which…
4
votes
1 answer

Calculate cyclomatic complexity at run time for generated program tree

I am running an evolutionary algorithm that automatically generates S-expressions that represent an abstract syntax tree. From there I generate C code to create a compilable program. For each generated expression I need to calculate the cyclomatic…
erik
  • 3,810
  • 6
  • 32
  • 63