Cyclomatic Complexity

To enhance understandability in our code, cyclomatic complexity must be limited. Cyclomatic complexity is related to the structural complexity of the code which being analysed. High complexity leads to a high number of defects and maintenance costs. In order to calculate cyclomatic complexity, we have to create a graph. Also this metric give us results related to testability.

The general formula to compute cyclomatic complexity is:

M = V(G) = E - N + 2P

Graf Terimleri
  • V(G) = Cyclomatic number of G
  • E = Number of edges
  • N = Number of nodes
  • P = Number of unconnected parts of the graph

If there are no branches, the cyclomatic complexity level is 1. As branches raise cyclomatic complexity levels rise.

        Code Highlighting Example         

#include 
1. void exampleFunction(int a, int b) {
2. if (a > b) {
3.       std::cout << "a is greater than b" << std::endl;
4.    } else if (a < b) {
5.        std::cout << "a is less than b" << std::endl;
6.   } else {
7.        std::cout << "a is equal to b" << std::endl;
8.    }
9. }
  
     

Two Condition : if (a>b) , else if (a

Total number of edges is 5

Total number of nodes is 6

The entire function is a single connected component , P = 1

V(G)=E – N + 2P

V(G)=5-6+2(1) = 1 This result gives that there are 1 distinct paths through the code.

References

REFERENCES:

(Internal Audit and IT Audit) Abu Sayed Mahfuz - Software Quality Assurance_ Integrating Testing, Security, and Audit-Auerbach (2021)

Stephen H. Kan - Metrics and Models in Software Quality Engineering-Addison-Wesley Professional (2003)

Capers Jones, Jitendra Subramanyam, Olivier Bonsignour - The Economics of Software Quality -Addison-Wesley (2011)

Murali Chemuturi - Mastering Software Quality Assurance_ Best Practices, Tools and Technique for Software Developers-J. Ross Publishing (2010)

Yorumlar