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.
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.
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.
Mentioned below is the sequential method for calculating Cyclomatic Complexity:
Calculating Cyclomatic Complexity is imperative as:
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.