Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculation of Cyclomatic Complexity [closed]

Tags:

I am at learning stage of cyclomatic complexity(CC). For practise, I am calculating cyclomatic complexity of 2 examples and want to confirm if my answers are correct or not...

Referring to wikipedia, CC is given by M = E − N + 2P where:

  • E = the number of edges of the graph
  • N = the number of nodes of the graph
  • P = the number of connected components

Please help.

Example 1

Here, E = 8, N = 9 and P = 1. Hence M = 8 - 9 + (2x1) = 1.

Example 2:

Example 2

Here E = 11, N = 10 and P = 1. Hence M = 10 - 11 + (2x1) = 1.

Hence for both the examples CC is 1. Please let me know if my calculation is correct or not.

like image 860
tech_human Avatar asked Feb 01 '12 14:02

tech_human


People also ask

What is the formula for calculating cyclomatic complexity?

3) Cyclomatic complexity V(G) = P +1 V (G) = 2 + 1 = 3 Where P is predicate nodes (node 1 and node 2) are predicate nodes because from these nodes only the decision of which path is to be followed is taken.

What is cyclomatic complexity and how it is calculated?

Cyclomatic complexity is a source code complexity measurement that is being correlated to a number of coding errors. It is calculated by developing a Control Flow Graph of the code that measures the number of linearly-independent paths through a program module.

How is cyclomatic complexity calculated in 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 the cyclomatic complexity of the following code?

The cyclomatic complexity calculated for above code will be from control flow graph. The graph shows seven shapes(nodes), seven lines(edges), hence cyclomatic complexity is 7-7+2 = 2. Use of Cyclomatic Complexity: Determining the independent path executions thus proven to be very helpful for Developers and Testers.


2 Answers

You need to take more care to correctly insert the values into the formula.

In example 1, you say

Here, E = 8, N = 9 and P = 1

But actually, it's the other way round: 9 edges (=E), 8 nodes (=N), so you get a CC of 3.

In example 2, you have the values right: E=11, N=10, P=1. But you insert them in the wrong order in the formula; it actually should be 11 - 10 + (2x1) = 3.

Shortcut: If you have a picture of your graph, you can very easily determine the cyclomatic complexity. Just count the number of regions the background is divided into by the edges. In your first example, you have 2 inner regions (bordered by the edges) and one surrounding region, giving a CC of 3. Same goes with the second example. (This method requires that edges are not crossing each other, obviously.)

like image 200
flyx Avatar answered Oct 21 '22 13:10

flyx


Also if this helps, it the number of conditional (If, while, for) statements +1. So in the above example, there are 2 conditional statements . so 2+1=3. Cyclomatic complexity in this case is 3

like image 22
Neethi Avatar answered Oct 21 '22 14:10

Neethi