Back

Cyclomatic Complexity

Idealogic’s Glossary

Cyclomatic Complexity (CC) is one of the software metrics that can be used to measure the level of the program’s complexity based on the number of paths that the program can take. This metric is one of the most useful ones that measure the number of decisions that are being taken within the program and it can also show the number of loops, conditions and function calls within the code. CC is a measure that tells developer how much effort is going to have to put in to test a given piece of code.

How is Cyclomatic Complexity Calculated?

Cyclomatic Complexity is based on Graph Theory and the calculation is done with help of Control Flow Graphs, in these graphs:

  • Nodes are the elements of the program which can be a program statement or instruction.
  • Edges symbolize the control flow between these statements.

The Cyclomatic Complexity  can be calculated using the formula   CC = E – N + 2P,   where:

  • E is the number of edges in the control flow graph.
  • N is the number of nodes in the graph.
  • P is the number of connected components; it is usually 1 for a single program.

Significance of Cyclomatic Complexity

A high CC means that the code contains a lot of branches and, therefore, there is a high possibility of errors. It is easier to work with simple code than with complex code because the former is easier to read, modify, and debug. Therefore, this metric is valuable in several ways:

  • Code Reviews: This metric may be used to determine the sections of the code that may be over complex and should be simplified or optimized. In code review, high complexity may point to the fact that the code needs to be refactored in order to be more readable and less complicated.
  • Testing: The metric is employed to find out the least amount of test cases that is needed in order to cover all the possible paths in the program. The higher the complexity, the more test cases are required in order to achieve the proper level of testing and this is important for the stability of the software.
  • Refactoring: CC can be used in helping developers in the process of refactoring. This means that the code is easier to comprehend, has fewer likelihood of containing errors and is easier to modify.

Although the Cyclomatic Complexity is higher in certain cases it may indicate the existence of problems, this doesn’t mean that complex code is always wrong. There are certain programs which inherently contain intricate decision making structures. However, this high complexity should make one to take some time and make sure that the complexity is required and that the code is well organized and documented.

In general, high Cyclomatic Complexity can lead to:

  • Increased risk of defects: More paths through the code can be equal to more possibilities of bugs.
  • Greater testing effort: As code becomes more elaborate, there is the need to test all possible paths that the code may take.
  • Difficult maintenance: Code that is complicated is less easy to comprehend and alter and hence there is a high possibility of making new faults during the maintenance process.

Conclusion

Cyclomatic Complexity is one of the most important software metrics that define the program’s complexity based on the number of control flow paths. It is a very helpful technique to find the code fragments that might be prone to issues and channel the code review, testing, and refactoring activities. On the downside, high complexity might mean more difficult and prone to errors code but on the other hand it is possible to enhance the quality of the software by reducing complexity of the structures and making sure that all possible defects have been tested.