Cyclomatic complexity is a software metric that measures how complex a program’s control flow is. Introduced by Thomas McCabe in 1976, it quantifies the number of independent paths in a codebase. In simple terms, it helps determine how difficult a program is to test, maintain, and debug by counting the different execution paths that can be taken.
For a better understanding, consider the number of different routes a car can take through a city. If it is on a simple road with no intersections, then it can only travel in one direction, making navigation easy. However, if the road has multiple turns, intersections, and traffic signals, the number of possible routes increases, making navigation more complex.
Cyclomatic complexity applies this concept to code, measuring the number of possible autonomous paths in a program.
Take a look at the Python script below:

Figure 1: A short Python script with low complexity
In the script above, there are four possible paths and three decision points (if, elif, and else), resulting in a cyclomatic complexity of four (one path for each condition and one for the default case).
- If the status is “pending,” it prints “Order is pending.”
- If the status is “shipped,” it prints “Order has been shipped.”
- If the status is “delivered,” it prints “Order has been delivered.”
- If none of these match, it prints “Invalid order status.”
Since there are only a few decision points, the cyclomatic complexity is low.
Now, compare it to the script below:

Figure 2: A Python script with higher complexity
In this script, there are seven possible paths, increasing the complexity:
- If the order is “pending” and the payment is completed, it prints “Order confirmed and ready for shipment.”
- If the order is “shipped” and tracking information is available, it prints “Order in transit. Tracking ID: [tracking]”
- If the order is “delivered” and customer feedback is present, it prints “Order delivered with customer feedback.”
- If the order is “canceled” and the refund has been processed, it prints “Order was canceled and refund has been processed.”
- If the order is “returned” and a return reason is given, it prints “Order returned due to [reason].”
- If the order is “failed” and the payment was unsuccessful, it prints “Order failed due to payment failure.”
- If none of these conditions match, it prints “Order status unclear or invalid.”
So, this script contains multiple conditions and nested checks, thereby increasing the number of possible paths.
Cyclomatic complexity is a comprehensive approach based purely on the number of independent paths in the code, but that doesn’t always mean a longer script is more complex or that a short script is always simple. A short script with deeply nested conditions, multiple logical operators in a single statement, or intricate loops can still have a high cyclomatic complexity.
For example:

Figure 3: A shorter script with higher complexity
This function is short, but the complexity is higher because it has multiple logical conditions. Even though there aren’t many lines of code, the number of decision paths remains high, making it harder to read, test, and debug.
The script classifies a number as positive even, positive odd, negative even, negative odd, zero, or invalid using nested ternary operators. It first checks whether the number is positive or negative, then determines if it’s even or odd based on its value and divisibility by 2.
If the number is greater than 0, it checks whether it’s positive even (2, 4, 6) or positive odd (1, 3, 5). If it’s less than 0, it checks whether it’s negative even (-2, -4, -6) or negative odd (-1, -3, -5). If the number is zero, it returns “zero,” and any unexpected input defaults to “invalid.” This complexity comes from the multiple nested conditions, creating multiple execution paths.
Cyclomatic complexity itself isn’t just about length; it’s about the structure and how complex code is to test and maintain. Just because code has fewer paths doesn’t mean it’s easy to read.
How to measure cyclomatic complexity
Cyclomatic complexity is measured using the formula: M=E−N+2P
Where:
- M = Cyclomatic complexity
- E = Number of edges (connections) in the control flow graph
- N = Number of nodes (decision points and statements)
- P = Number of connected components (typically 1 for a single function)
Steps to measure cyclomatic complexity
- Create a control flow graph: Write clearer codes and represent them as a flowchart where each control flow statement creates new branches.
- Identify nodes and edges: Count the decision points (nodes) and the connections between them (edges).
- Apply the formula: Plug the values into M = E – N + 2P to compute the complexity score.
- Interpret the result: A higher value indicates more complex logic, making the code harder to test and maintain.