Cyclomatic complexity count of the number of decisions in the source code. The code is complex if the number of decisions. It is used to specify the complexity of code. It is calculated using the Control Flow Graph of the code which measures the number of linearly independent paths.
Code complexity calculation
It can be calculated using the formula :
C = E – N + 2P
P = number of disconnected components of the flow graph
E = number of edges (transfers of control)
N = number of nodes (a sequential group of statements)
Why it is important?
- Code complexity identified has to go through reviews and additional dynamic testing.
- It also helps to identify the test cases.
- It reduces the coupling of code.
- Remove unnecessary/redundant code
Steps to follows to determine code complexity
- Identify nodes and edges from code and construct a graph
- Identify all the independent paths from the graph
- Calculate the code complexity using the formula mentioned above
Use of Cyclomatic Complexity:
- It helps to identify the independent path executions which would be very helpful for Developers and Testers.
- It helps in identifying the uncovered path of the software.
- It helps in increasing the code coverage.
- It helps to evaluate the risks associated with software
- It ensures that every path of the software has been tested at least once.
- These metrics being used earlier in the program helps in reducing the risks.
Advantages of Cyclomatic Complexity
- It helps in identifying the concentrating areas which are best for testing.
- It is helpful in guiding the testing process.
- It can be used as a quality metric, gives relative complexity of various designs.
- It can be applied easily.
Disadvantages of Cyclomatic Complexity:
- It can only measure the code complexity and not data complexity.
- The nested conditional structures used in code are harder to understand as compared with non-nested structures.
- The complexity of simple comparisons and decision structures sometimes provides a misleading figure.