I'm trying to calculate the NPath Complexity within a function, but I'm unsure what parts I count.
My understanding is that it'll sum:
- If
- Else
- Else If
- Switch
- Case
- Default
- Do
- While
Is this correct? Or am I missing something?
You also need to include operators such as && || and ? :, since these themselves can produce alternate execution paths.
for out of your list of control-flow statements ...
try/catch/finally...
The NPath Complexity is the sum of the possible routes through your code (ignoring cycles).
The complexity therefore depends on the structure of your code rather than just the tokens:
if (x) {
if (y) {
// Do something
} else {
// Do something else
}
} else {
// Do something even more else
}
Has 3 possible paths that could be taken, depending on the values of x and y.
As you nest control structures more deeply, the number of possible paths increases exponentially.
Skimming this code: http://pmd.sourceforge.net/pmd-4.3/xref/net/sourceforge/pmd/rules/design/NpathComplexity.html will give an idea of the complexity of calculating the complexity:-)