First, what is cyclomatic complexity and how is it used in software engineering?
Thomas McCabe coined the term in 1976, and according to Thomas, it represents the measure of the decision logic in a single software module, to be more precise. It’s a software metric that gives the quantitative measure of the logical complexity of the program. It also can be explained as the measurement tool for the number of linearly-independent paths through a program module.
Teams can segregate the function into two primary uses:
The McCabe methodology or The McCabe metric is based on graph theory and mathematically analyzes the software's structure or design.
Figure 1. Three methods of evaluating the cyclomatic complexity of the graph. Riabov, Vladimir. (2007). Graph Theory Applications in Developing Software Test Strategies for Networking Systems. Rivier Academic Journal. 3. 1-13.
As shown in Figure 1, several approaches for computing and evaluating code complexity exist. But the formula that’s most mentioned and propagated through the rest of the literature and technical articles is:
V (G) = E - N + 2
E represents the total number of edges in the flow graph. N represents the total number of nodes in the flow graph.
Let’s compare the code example with the design flow graph to analyze further and understand the formula.
Figure 2. Comparison of the design flow graph with code example.
In Figure 2, we can see the comparison of the design flow graph with the code example. If we further break down the comparison values by assigning values to the variables that represent number of edges (E) and number of nodes (N), we get 7 edges and 6 nodes.
This results in a cyclomatic complexity of 3:
V(G) = E – N + 2 => V(G) = 7 – 6 + 2 = 3;