Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Cyclomatic Complexity?

A term that I see every now and then is "Cyclomatic Complexity". Here on SO I saw some Questions about "how to calculate the CC of Language X" or "How do I do Y with the minimum amount of CC", but I'm not sure I really understand what it is.

On the NDepend Website, I saw an explanation that basically says "The number of decisions in a method. Each if, for, && etc. adds +1 to the CC "score"). Is that really it? If yes, why is this bad? I can see that one might want to keep the number of if-statements fairly low to keep the code easy to understand, but is this really everything to it?

Or is there some deeper concept to it?

like image 639
Michael Stum Avatar asked May 26 '09 16:05

Michael Stum


People also ask

Which is cyclomatic complexity?

Cyclomatic complexity is a measurement developed by Thomas McCabe to determine the stability and level of confidence in a program. It measures the number of linearly-independent paths through a program module. Programs with lower Cyclomatic complexity are easier to understand and less risky to modify.

What is cyclomatic complexity white box testing?

Cyclomatic Complexity: It is a measure of the logical complexity of the software and is used to define the number of independent paths. For a graph G, V(G) is its cyclomatic complexity. Calculating V(G): V(G) = P + 1, where P is the number of predicate nodes in the flow graph.

What is cyclomatic complexity formula?

Method 1: Total number of regions in the flow graph is a Cyclomatic complexity. Method 2: The Cyclomatic complexity, V (G) for a flow graph G can be defined as. V (G) = E - N + 2. Where: E is total number of edges in the flow graph. N is the total number of nodes in the flow graph.

What is cyclomatic complexity black box testing?

Answer: b. Explanation: Cyclomatic complexity measures the amount of decision logic in the program module. Cyclomatic complexity gives the minimum number of paths that can generate all possible paths through the module.


2 Answers

I'm not aware of a deeper concept. I believe it's generally considered in the context of a maintainability index. The more branches there are within a particular method, the more difficult it is to maintain a mental model of that method's operation (generally).

Methods with higher cyclomatic complexity are also more difficult to obtain full code coverage on in unit tests. (Thanks Mark W!)

That brings all the other aspects of maintainability in, of course. Likelihood of errors/regressions/so forth. The core concept is pretty straight-forward, though.

like image 149
Greg D Avatar answered Oct 12 '22 08:10

Greg D


Cyclomatic complexity measures the number of times you must execute a block of code with varying parameters in order to execute every path through that block. A higher count is bad because it increases the chances for logical errors escaping your testing strategy.

like image 45
Tetsujin no Oni Avatar answered Oct 12 '22 07:10

Tetsujin no Oni