Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is NPath Complexity and how to avoid it?

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?

like image 984
Wolverine789 Avatar asked Jul 10 '14 07:07

Wolverine789


People also ask

What is NPath complexity?

NPath complexity is the number of execution paths possible for a given code i.e. the number of ways the given code can get executed (ignoring cycles).

What is cyclomatic complexity and how do you reduce it?

Cyclomatic complexity refers to the number of possible execution paths inside a given piece of code—for instance, a function. The more decision structures you use, the more possible branches there are for your code. Cyclomatic complexity is especially important when it comes to testing.


1 Answers

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
like image 155
JohannesDienst Avatar answered Oct 04 '22 06:10

JohannesDienst