NPath complexity

NPath complexity is very much like cyclomatic complexity in that it also calculates the amount of paths. Or possible outcomes, rather.

if ($user->getSalutation()) {
    echo $user->getSalutation().' ';

if ($user->getName()) {
    echo $user->getName();
} else {
    echo 'anonymous';

The above code example has a cyclomatic complexity of 3 (2 if's and an else), but because of how they're structured, there are 4 possible outcomes:

  • $salutation $name
  • $name
  • $salutation anonymous
  • anonymous

The combination of multiple decision paths can compound in multiple different outcomes. Especially when nested, the amount of possible outcomes can increase exponentially. Then imagine the amount of tests it takes to test every possible outcome.