Cyclomatic complexity is a measure of how many linearly independent paths there are through a program's code.
Consider a simple program that contains several conditional checks and function invocations:
if (a) {
alpha();
if (b) bravo();
if (c) charlie();
}
if (d) delta();
Even in this misleadingly simple piece of code, nine distinct paths can be taken. So, depending on the values of a, b, c, and d, there are nine possible sequences of alpha, bravo, charlie, and delta that will run:
- alpha()
- alpha() and bravo()
- alpha(), bravo(), and charlie()
- alpha(), bravo(), charlie(), and delta()
- alpha(), bravo(), and delta()
- alpha() and charlie()
- alpha(), charlie(), and delta()
- alpha() and delta()
- delta()
A high level of cyclomatic complexity is undesirable. It can lead to the following:
- Cognitive burden: Cyclomatically complex code can be difficult for...