19

In this line:

public Map getAll(BusinessTargetPK pkBusinessTargetId) throws Exception

I am getting this error:

NPath Complexity is 32,768 (max allowed is 200)

And in this line:

public Map getAll( Long  RLE_ROLE_ID  ) throws Exception {

I get this error:

The method getAll() has an NPath complexity of 2048

I am completely unaware of what is NPath Complexity and what it means.

Can someone give advice how to avoid this type of error?

Jani Uusitalo
  • 226
  • 1
  • 9
Wolverine789
  • 407
  • 2
  • 6
  • 10
  • "The NPath complexity of a method is the number of acyclic execution paths through that method." – Erik Pragt Jul 10 '14 at 07:17
  • @Wolverine789 You have also recently posted another question about a message from Sonar. There ought to be a documentation explaining about these error messages - get it from the person responsible for setting up your development environment! – laune Jul 10 '14 at 07:37
  • Actually there is no documentation..I am the only one who is doing this bug fixes for my project in SonarQube..Hence this NPath Complexity is new to me.. – Wolverine789 Jul 10 '14 at 09:15
  • 2
    The codingswag article mentioned above is no longer available. It leads to a page that reads "The Free Trial is Over". – Kuro Dec 29 '15 at 17:30
  • I added a `try/catch` in a method to resolve a bug, and the NPath complexity went +100. Is it really only related to acyclic execution? – Christian Vincenzo Traina Mar 16 '23 at 10:19

2 Answers2

27

This Link: https://modess.io/npath-complexity-cyclomatic-complexity-explained/

explains it very well as:

The NPath complexity of a method is the number of acyclic execution paths through that method.

This means you should avoid long functions with a lot of (nested) if/else statements.

So my advice would be:

  1. Split your functions into smaller ones
  2. Eliminate useless if/else-statements where possible
JohannesDienst
  • 455
  • 4
  • 11
  • 4
    The NPath complexity of a method is the number of acyclic execution paths through that method or The simple explanation is that how many "paths" there are in the flow of your code in the function. – Wolverine789 Jul 10 '14 at 07:34
8

This is an old thread and Wolverine789 has probably figured out the answer by now but for those who still find this thread in the Google search results, I found the following description of the error by Niklas Modess helpful:

https://modess.io/npath-complexity-cyclomatic-complexity-explained/