Cyclomatic Complexity Code Metrics
Cyclomatic complexity is a software metric used to indicate the complexity of a program.
It was developed by Thomas J. McCabe, Sr. in 1976.
Cyclomatic Complexity is the number of paths through the source code as well as the number of decisions made in your source code.
More the decisions made(or more the code paths) in source code, the more complex it is.
Higher the cyclomatic complexity, more difficult is the code to test, maintain and troubleshoot.
Each function in the source code has a minimum complexity of 1. This calculation varies slightly by language because keywords and functionalities do.
Cyclomatic complexity general score guidelines:
To reduce the cyclomatic complexity consider below:
- Break the function/method into smaller functions/methods by following single responsibility principle or inheritance — this way you will also prevent long functions/methods.
- Usage of Generics, common utility methods.
- Smaller independent functions with parallel execution.
References and further Reading