Skip to main content



  1. Home >
  2. Products & Services >
  3. PGRelief C/C++ >
  4. Product Details >
  5. McCabe Cyclomatic Complexity

PGRelief C/C++
McCabe Cyclomatic Complexity

Thomas McCabe proposed that the complexity of a program is determined by the complexity of its control structure, and this complexity is measured by counting the number of basic paths in the program. Complexity V(G) is calculated by the formula V(G) = π+1, where the number of judgment conditions is represented by π.

There are two types of complexity: TYPE 1 and TYPE 2. TYPE 1 complexity ignores complex conditions such as those contained in if clauses (such as && and ||), when counting judgment conditions. TYPE 2 complexity counts them as separate conditions when counting judgment conditions.

To make a program easy to understand, the recommended value for TYPE 1 complexity is 20 or less for each function. Generally, a program with a value of more than 20 for TYPE 1 complexity is said to take a long time to understand, and hardly anyone can understand a program that has a value over 50, except for the person who coded it.

Complexity calculation example

A calculation of TYPE 1 complexity counts a complex condition like that in the if clause of (a) (condition expressed using && or ||) as one branch. In the above example, π = 3 and the complexity is 4.

A calculation of TYPE 2 complexity counts each condition in a complex condition as one branch. In the above example, π = 4 and the complexity is 5. The following figure shows the paths in a TYPE 2 calculation. You can see that the number of paths is 5.