McCabe's cyclornatic complexity metric (MCCA76) is defined as:
- M = L — N + 2P
L = the number of links in the graph
N = the number of nodes in the graph
P = the number of disconnected parts of the graph (e.g., a calling program and a subroutine)
Cyclomatic complexity provides some useful rules of thumb:
- 1. Bugs per line of code increase discontinuously for M greater than 10.
- 2. Arbitrary modularity rules based on length, when applied to straight-line code that has few calls or only one call, increase rather than reduce complexity.
- 3. The amount of design, code, test design, and test effort is better judged by cyclomatic complexity than by lines of code.
- 4. Routines with high complexity, say 40 or more, should be given close scrutiny, especially if that complexity is not due to the use of case statements or other regular control structures. If the complexity is due to loops and raw logic, consideration should be given to subdividing the routine into smaller, less complex segments in order to avoid the nonlinear increase in effort associated with high complexity.