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
4
votes
2 answers

Annotations for enforcing cyclomatic complexity and LCOM constrains

At work we are using several tools to capture several metrics (mostly cyclomatic complexity and LCOM). We use those to get warning flags and to guide pre-emptive refactoring efforts. It's been very beneficial in increasing code quality. However, the…
luis.espinal
  • 10,331
  • 6
  • 39
  • 55
4
votes
4 answers

C function only called once and cyclomatic complexity

I think this question is more about style: I have an algorithm that has a very high CC (and a lot of lines!). I want to reduce it, and it's easy since there are pieces of code that can be grouped. The problem is that doing things this way I would…
4
votes
2 answers

Common causes of Cyclomatic Complexity and their solutions

At work we are looking into common problems that lead to high cyclomatic complexity. For example, having a large if-else statement can lead to high cyclomatic complexity, but can be resolved by replacing conditionals with polymorphism. What other…
Daniel
  • 16,026
  • 18
  • 65
  • 89
4
votes
2 answers

How to resolve CA1502: Avoid excessive complexity?

As per code analysis result, the following is the warning message, CA1502 Avoid excessive complexity 'METHOD()' has a cyclomatic complexity of 27. Rewrite or refactor the method to reduce complexity to 25. BusinessServices ReportService.cs …
4
votes
1 answer

Understanding cyclomatic complexity

I have some piece of code that basically looks like this: public MyObject getData(boolean someFlag) { String select1 = "SELECT * FROM myTable WHERE someInteger = ?"; SqlHostvariablen hostvars = new SqlHostvara(); …
André Stannek
  • 7,773
  • 31
  • 52
4
votes
1 answer

How does a static Dictionary have cyclomatic complexity?

I have the following class: public static class MyClass { private static readonly Dictionary> valueTypes; static MyClass() { var dictionary = new Dictionary>(); …
cubetwo1729
  • 1,438
  • 1
  • 11
  • 18
4
votes
2 answers

Measuring Cyclomatic Complexity of Data Mapping Functions & Closures

According to the basic rules of cyclomatic complexity, the below code should have a complexity of 2 (only one branch point - the for loop). function SumArray(array_to_sum) { var sum = 0; for (var i = 0; i < array_to_sum.length; i++) { …
MightyE
  • 2,679
  • 18
  • 18
4
votes
1 answer

Dynamic keyword causes Cyclomatic Complexity > 25 in Visual Studio 2010 Code Analysis

I have a piece of code that is similar to this: dynamic a = new ValueHolder(); dynamic b = new ValueHolder(); dynamic c = new ValueHolder(); a.MtdActual = b.MtdActual + c.MtdActual; a.YtdActual = b.YtdActual + c.YtdActual; a.MtdVariance =…
Andre
  • 3,717
  • 4
  • 25
  • 29
3
votes
1 answer

Full lists of expression for C# code which +1 for Cyclomatic Complexity

I need to construct a Control Flow Diagram (simple flow graph with nodes and edges) for each method in my C# project in order to demonstrate the graph-way to calculate cyclomatic complexity. I first counted the cyclomatic complexity using VS 2010,…
shennyL
  • 2,764
  • 11
  • 41
  • 65
3
votes
0 answers

How to reduce cyclomatic complexity on a reactive code?

I have a microservice using Spring Webflux that makes some requests to another services. The code looks like this: public class ExampleService { @Autowired private WebClient webClient; …
3
votes
1 answer

in the sake of increasing code performance... ESLint is reporting "Arrow function has a complexity of 21. Maximum allowed is 20"

ESLint reports an intolerably high complexity. I want to know why is it too complex? And what happens if I just split it into multiple functions - is that performant? As I know we always have to write minified code so if we split it into multiple…
MansouriAla
  • 177
  • 1
  • 1
  • 11
3
votes
4 answers

What is (or should be) the cyclomatic complexity of a virtual function call?

Cyclomatic Complexity provides a rough metric for how hard to understand a given function is, or how much potential for containing bugs it has. In the implementations I've read about, usually all of the basic control flow constructs (if, case,…
3
votes
1 answer

How to disable radon high cyclomatic complexity check?

Our codebase is checking high cyclomatic complexity using Radon. I have a function which is triggering this linter error, but I would like for it to pass the linter, similar to something like a pylint disable. Is there a way to do that with radon?
Arya
  • 1,382
  • 2
  • 15
  • 36
3
votes
2 answers

How do I reduce the cyclomatic complexity?

Thanks for reading my question. I am currently taking a Java class on Coursera, and was asked to write a program on minesweeper for the assignment. My code creates the correct result, but my grade was deducted greatly because my code is ”excessively…
3
votes
1 answer

What design pattern is suitable for handing many conditional branching

I have a a bunch of rows in a table which store some variables and associated values. I have to fire a task depending the values of the variables from different rows. Multiple values from multiple rows can be overlapped by AND/OR conditions. What…
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256