• Software Testing Dictionary
  • Home

Software Testing - Cyclomatic Complexity



Software testing comprises both white box and black box testing techniques. In white box testing, the internal structure of the code is verified. Cyclomatic Complexity is one of concepts under white box testing, and is developed by Thomas McCabe. It is a software metric to measure the program complexity by getting the total number of the decision points.

What is a Cyclomatic Complexity?

The cyclomatic complexity is obtained by drawing a control flow graph of the code, and it calculates the total number of linearly independent paths running through the complete program. Higher the cyclomatic complexity number, higher is the risk to update and maintain code, higher is the probability of finding issues, and more difficult it is to understand the code. The cyclomatic complexity measures the readability of the code, and total count of distinct, linearly-independent paths in the program which gives an idea of how complicated the implementation logic is.

Formula to Calculate the Cyclomatic Complexity

There are three formulae to calculate the cyclomatic complexity as listed below −

Formula 1

For a program code control graph(G), the cyclomatic complexity denoted by V(G) is equal to E - N + 2 * P, where E is equal to the total number of edges, N is equal to the number of nodes, and P is equal to the total number of connected components in the graph.

Formula 2

For a program code control graph(G), the cyclomatic complexity denoted by V(G) is equal to P + 1, where P is equal to the total number of decision or conditional nodes. The decision or conditional nodes result in bringing out two branches in a control flow graph.

Formula 3

For a program code control graph(G), the cyclomatic complexity denoted by V(G) is equal to the R + 1, where R is the total count of the closed regions within the control flow graph.

Example

Let us take an example of the below block of code from where we will calculate the cyclomatic complexity using the formulae discussed above.

IF X = 300
   THEN IF Y > Z
   THEN X = Y
ELSE X = Z
   END IF
END IF
PRINT X

The control flow graph of the above lines of code is shown below −

cyclomatic complexity graph

In the above control flow graph, there are seven nodes(N) denoted by blue circles. Please note, the total number of lines in the code is equal to the total number of nodes. There are eight edges(E) in the control flow graph denoted in red, and since there is only one method, the total number of connected components(P) in the graph is 1. So as per the formula,

V(G) = E - N + 2 * P
 = 8 - 7 + 2 * 1 = 3

In the above code and its control flow graph, there are two conditional statements, IF X = 300, and THEN IF Y > Z, hence there are two conditional nodes(P) namely, 1, and 2, denoted in green in the control flow graph. So as per the formula,

V(G) = P + 1
 = 2 + 1 = 3

In the above control flow graph, there are two closed regions(R), denoted by two transparent circles namely 1, and 2 denoted in orange in the control flow graph. So as per the formula,

V(G) = R + 1
 = 2 + 1 = 3

So using the three formulae, we have obtained the same value of cyclomatic complexity as 3.

Uses of Cyclomatic Complexity

The uses of the cyclomatic complexity are listed below −

  • Detecting all the independent paths prove to be very helpful for both the developers and testers.
  • It confirms that every path in the code is verified at least once.
  • It helps to detect the uncovered paths in the program code.
  • It assists in improving the code coverage.
  • It aids in identifying the potential risks and to mitigate them.

Advantages of Cyclomatic Complexity

The advantages of the cyclomatic complexity are listed below −

  • It is used as a quality metric for the code developed for the software.
  • It is faster than the Halstead metric.
  • It helps to determine the effort and critical areas for testing.
  • It steers the testing process.
  • It can be applied easily.

Disadvantages of Cyclomatic Complexity

The disadvantages of the cyclomatic complexity are listed below −

  • It is only concerned with complexity of the software code and not the data it handles.
  • The cyclomatic complexity value for nested program code is not easily determined.
  • The cyclomatic complexity may bear false value for simple comparisons and decision structures.

Conclusion

This concludes our comprehensive take on the tutorial on Software Testing Mccabe's Cyclomatic Complexity. We’ve started with describing what is a cyclomatic complexity, formula to calculate the cyclomatic complexity, an example to obtain the the cyclomatic complexity, what are the uses of the cyclomatic complexity, what are the advantages of the cyclomatic complexity, and what are the disadvantages of the cyclomatic complexity. This equips you with in-depth knowledge of Software Testing - Mccabe’s Cyclomatic Complexity. It is wise to keep practicing what you’ve learned and exploring others relevant to Software Testing to deepen your understanding and expand your horizons.

Advertisements