2

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?

0

2 Answers 2

2

You also need to include operators such as && || and ? :, since these themselves can produce alternate execution paths.

7
  • You also left for out of your list of control-flow statements ... Commented Sep 3, 2014 at 12:43
  • actually take a look here leepoint.net/notes-java/principles_and_practices/complexity/… the term is "cyclomatic complexity", there's actually a few other things I forgot, such as try/catch/finally... Commented Sep 3, 2014 at 12:51
  • npath and cyclomatic are different types of complexity and are measured differently. Commented Sep 3, 2014 at 14:06
  • Okay, I looked this up. Seems N-path takes into account the possible outcome of all operations, but OP only mentions flow-control. Do you think he meant CC? Commented Sep 3, 2014 at 14:33
  • Its possible... though it probably needs more clarification to determine exactly what the OP is after. Commented Sep 3, 2014 at 14:43
1

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:-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.