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

How is cyclomatic complexity counted?

I'm reading Kent Beck's “TDD by Example” and I can't understand why the cyclomatic complexity of his program is fractional. In Wikipedia the complexity is defined as M = E − N + 2P where E, N and P are integers.
1
vote
1 answer

How to reduce cyclomatic complexityfor a method returning the name of the month?

There's a metrics plugin in eclipse that measures cyclomatic complexity by counting the decisions (independent paths), specifically: (if, for, while, do, case, catch and the ?: ternary operator, as well as the && and || conditional logic operators…
Carlo Otto
  • 47
  • 1
  • 5
1
vote
1 answer

Resolving Cyclomatic and Perceived Complexity

I have the following method in my test app: def on(definition, visit = false, &block) if @page.is_a?(definition) block.call @page if block return @page end if @context.is_a?(definition) block.call @context if block @page =…
Jeff Nyman
  • 870
  • 2
  • 12
  • 31
1
vote
2 answers

Detect high cyclomatic complexity limit before check in code

I would like to know what sort of tools are available to detect bad code (high cyclomatic complexity limit) before checking code in. Working on a legacy project and there is so much spaghetti code already. Sonar is not necessarily helpful in this…
aug70co
  • 3,965
  • 5
  • 30
  • 44
1
vote
2 answers

Decreasing the Cyclomatic Complexity without affecting the business logic

consider this method: public ActionResult DoSomeAction(ViewModel viewModel) { try { if (!CheckCondition1(viewModel)) return Json(new {result = "Can not process"}); if (CheckCondition2(viewModel)) { …
Codehelp
  • 4,157
  • 9
  • 59
  • 96
1
vote
0 answers

PHP, optimizing for Boolean operators and functions, redundancy and cyclomatic complexity

I'm looking at optimizing, formatting and styling a few largish files at the moment. Most of it seems to not be optimally written (probably airing on the side of caution rather than whats best). One of the things I've specifically been trying to do…
1
vote
1 answer

Is do while affect cyclomatic complexity?

For example I got a code: int a = 3; if(a < 0) { System.out.println(“a < 0"); } else if (a == 0) { System.out.println(“a == 0"); } else { do{ System.out.println(“Loop never end"); }while(true) } return a; Cyclomatic…
user3489985
  • 327
  • 4
  • 18
1
vote
2 answers

How program that efficient form method with multiples parameters

I have method public List getListaDataTable(param1, param2, param3) Method must call to others method depends witch params is null. If I do it with if ... else I going to do 7 condition. Any idea how resolve this? THX
Mathew Rock
  • 989
  • 2
  • 14
  • 32
1
vote
1 answer

Is Cyclomatic Complexity of '267' better?

I have developed an application in .NET using C#. Using 'Calculate code metrics' option, I got the cyclomatic complexity score of '267' with 2,348 lines of code and Depth of Inheritance = 7, Class Coupling = 150 and Maintainability index = 80. I…
Lalit Rao
  • 551
  • 5
  • 22
1
vote
2 answers

How to analyze unit test *path* coverage

I like to work with unittests and I think they help me alot. I use dotcover to analyze my coverage but as you know code coverage is not everything but I think it's still a vital tool. Right now I'm working on a project with really high complexity…
1
vote
1 answer

C static array initialization and cyclomatic complexity

I have the following code: typedef struct A_{ void* values; }A; typedef struct C_{ int array_C[3][3]; }C; typedef struct B_{ C c; }B; int main(void) { B* b; A array_A[2] = { { …
IceCube
  • 405
  • 1
  • 5
  • 12
1
vote
3 answers

McCabe-style function complexity test for Mac

What tool can I use to test McCabe-style function complexity of my code on Max OS X? There is pmccabe for Linux, which is on my department's machines and what they want me to use. It analyzes each function in certain project files and spews out data…
Joseph Hansen
  • 12,665
  • 8
  • 50
  • 68
1
vote
4 answers

How can I reduce Cyclomatic complexity?

I have a method like below. Please help to avoid Cyclomatic complexity. private double getSum(Data data) { double total = 0; if(parameters.getParam1()) total += data.getParam1(); if(parameters.getParam2()) total +=…
user3222372
  • 352
  • 3
  • 20
1
vote
1 answer

Computing number of independent paths

Should the boolean expressions joined using && be considered as separate statements while computing number of independent paths? For example, Line 3 has two conditions, and if the first expression was false, then the subsequent expression would be…
MintUser
  • 103
  • 1
  • 8
1
vote
1 answer

Why there is a change in cyclomatic complexity when rearranging switch statements with other dependent factors?

The CC for the below method would be generated as 9 Public Enum Fruits Apple Pineapple Banana PassionFruit Orange Melon Grape Mango End Enum Private Function Fruity(ByVal…
kurozakura
  • 2,339
  • 8
  • 28
  • 41