Understand the CountPath Metric

So imagine you’re reviewing metrics for a small function:

  61 lines of code, pretty small, keeping functionality nice and modular. Good.

Cyclomatic complexity of 30. Highly complex, but within the acceptable range. Good.

CountPath metric 536,870,912. What is this? 536 Million paths in 61 lines of code, that can’t be right, can it?!?

The CountPath metric counts the number of unique paths though a body of code. It’s pretty common to have a huge count path metric, even in a small body of code.

In this example, I pull up the code and it looks like this:

image

29 IF statements in a row. That’s all, logically quite simple code:

image

But the fun thing about IF statements, is every one doubles the number of potential paths in your code(2n) – and as we all remember from our algebra days, exponential growth happens very fast.

So 2 IF statements makes 4 paths. 23 is 8 paths. let me demonstrate:

image

So for the 3 IF statements here, n=3 and 2n= 8 possible paths:

ACE
ACF
ADE
ADF
BCE
BCF
BDE
BDF

And any guesses what 229 is? Yep, 29 IF statements make 536,870,912 paths. Because this metric can grow so large, we do truncate it at 1 Billion, actually 999,999,999. If we see that many paths, we give up and stop counting. I don’t envy the QA team if they have to test every possible permutation of this, but at least you can believe Understand when it tells you that there are that many paths 🙂