Cyclomatic Complexity
Cyclomatic Complexity is a software metric introduced by Thomas J. McCabe in 1976 to measure the complexity of a program.
McCabe represented a computer program as a strongly connected directed graph, where:
- Nodes represent sequences of source code with no branches.
- Edges (arcs) represent possible control flow transitions during execution.
This program graph concept is used to measure and control the number of independent paths through a program, relating a program’s complexity to the topological complexity of its graph.
How to Calculate Cyclomatic Complexity
- McCabe proposed the cyclomatic number V(G) from graph theory as an indicator of software complexity
- It equals the number of linearly independent paths in the program’s control flow graph.
The formula is:
V(G)=E−N+2×P
Where:
- E = Number of edges in graph
- N = Number of nodes in graph
- P = Number of connected components in graph
Properties of Cyclomatic Complexity
- V(G) represents the maximum number of independent paths in the control flow graph.
- V(G)≥1 for any program.
- If V(G)=1, the graph has only one possible execution path.
- It is recommended to keep V(G)≤10 to maintain manageable program complexity.