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

Explain me the control flow of cyclomatic complexity

i = 0; n=4; //N-Number of nodes present in the graph while (i
-1
votes
2 answers

Reducing Cyclomatic Complexity with an interval

The following java code gives a rather high Cyclomatic Complexity. I would like to find a way to properly reduce it. How would I best do this? To be clear, the code gets the corresponding result of a value, based on whether the value is in between…
Patrigan
  • 553
  • 5
  • 7
-2
votes
1 answer

cyclomatic complexity: Need help in optimizing java code

Need help in optimzing this java code.The cyclomatic complexity is 20 need to be 15. private static void replaceNodeProperties(HashMap configNodeProperties, JSONArray esdNodepropertiesList) { HashMap replaceConfigNodePropRules = (HashMap)…
-2
votes
1 answer

Is there a way to increase the cyclomatic capacity of an input variable

I'm working on making a tic tac toe game and I have more complex code than my input statement can handle. This is for a project in my class and I've done some research. nothing to me makes sense there so I came here. I tried to make it less complex…
-2
votes
2 answers

Which is the more important code quality metric - between cyclomatic complexity or average fan out?

I was working on TICS code quality metrics for the first time, and I have this question. Many suggest breaking large functions into one or more functions in order to keep complexity less than 15. Doing so would increase number of functions called by…
-2
votes
1 answer

Need help in reducing the Cyclomatic complexity of LINQ query used for constructing JSON string

I have got 5 different sources of data(request, lsitCC, listSEDI, listSEDIFees and XMLRoot loaded into respective C# Array list objects). I need to construct a JSON request by combining the data from all of those sources based on certain conditions.…
Mahesh
  • 150
  • 1
  • 9
-3
votes
1 answer

Based on both Cyclomatic Complexity formulas, explain why the number of procedural nodes does not influence on cyclomatic complexity value

a. Based on both CC formulas, explain why the number of procedural nodes does not influence CC. b. What would happen if you had multiple stop nodes? d. What is the effect on cyclomatic complexity of break, continue, goto and return…
-3
votes
3 answers

Not using IF, FOR, CASE (...) for making decisions

I'm trying to write a procedure similar to the one below that would not use any of these expressions: if | while | for | case | default | continue | goto | && | || | catch | ternary operator ?: | ?? The reason is that when a plugin like metrics…
Carlo Otto
  • 47
  • 1
  • 5
1 2 3
19
20