Cyclomatic Complexity

Cyclomatic Complexity
Software development is not just about writing a code and launching it as an application. For once, writing the code is a technical job that cannot be done by every Tom, Dick and Harry and secondly, the codes are very complex in nature. This article will make it possible for you to understand how the complexity of a software is quantified by helping you learn more about Cyclomatic Complexity.

Meaning Of Cyclomatic Complexity

In technical terms, Cyclomatic Complexity is the metric measurement of a program's or source code's complexity. Herein, the metric is the quantitative measure of features that a software comprises of, these include quality, cost, size etc.

More About Cyclomatic Complexity

Thomas J. MaCabe developed the way to calculate Cyclomatic Complexity in the year 1976. According to his calculation, a programs Cyclomatic Complexity is directly proportional to the ease with which it can be modified and understood. Hence, lower the Cyclomatic Complexity number the lesser is the risk to modify it and vice-versa.

How is Cyclomatic Complexity Calculated

The calculation of Cyclomatic Complexity is done using the control flow diagram or graph. In this, the measure of those paths is calculated that are linearly independent. This means that there is no going back and backtracking. The formula for calculating Cyclomatic Complexity is:

 M = E – N + P


M is the Cyclomatic Complexity.

E is the total number of edges that occur in the control flow graph.

N is the total number of nodes that are there in the control flow graph.

P is the total number of nodes that have actual exit points.

The measurement of Cyclomatic Complexity need not always be done for the entire software. It can be broken down into methods, modules, and functions for ease of understanding.

Steps Involved In Calculating Cyclomatic Complexity

Mentioned below is the sequential method for calculating Cyclomatic Complexity:

  1. Firstly, study the code to build its graph with proper nodes and edges.
  2. Now build the appropriate graph.
  3. Identify all the paths that are independent.
  4. Calculate the Cyclomatic Complexity using the formula mentioned above.

Need For Calculating Cyclomatic Complexity

Calculating Cyclomatic Complexity is imperative as:

  • Designing Test Cases - it helps in designing of test cases. The more the number of paths the more will be the test cases in order to cover them all. This ensures that all the paths are tested at least once to uncover any flaws.
  • Better Code Coverage – if the program is big, Cyclomatic Complexity calculation allows for a more refined way to cover all those code areas that are critical. This makes for an improved way of covering the entire code.
  • Evaluating Associated Risk – the calculation for Cyclomatic Complexity gives an insight into the risk related to the program or software application. This way early calculation allows for reducing or minimizing risks.


As a software metric, Cyclomatic Complexity is very handy for white box testing. It is mainly used to calculate how complex the software or program is. If the Cyclomatic Complexity number is high then the chances of error are high and so is the risk associated with it.