I'm using PMD to generate some code quality report on a project.
I don't understand a result for the NPath complexity inspection.
I have created a dull class that is show-casing the result (this is not the real class, but it is using the same pattern):
import java.util.*; public class SOFExample { private final Map magicMap = new HashMap(); protected static final long UNKNWOWN = 0L; private static final class MyCal { long aTime; long bTime; long cTime; long dTime;} public void usefullMethod(final List myCals) { final Date a = magicMap.get("a"); final Date b = magicMap.get("b"); final Date c = magicMap.get("c"); final Date d = magicMap.get("d"); final long aTime = a == null ? UNKNWOWN : a.getTime(); final long bTime = b == null ? UNKNWOWN : b.getTime(); final long cTime = c == null ? UNKNWOWN : c.getTime(); final long dTime = d == null ? UNKNWOWN : d.getTime(); for (MyCal myCal : myCals) { if(myCal.aTime == UNKNWOWN) myCal.aTime = aTime; if(myCal.bTime == UNKNWOWN) myCal.bTime = bTime; if(myCal.cTime == UNKNWOWN) myCal.cTime = cTime; if(myCal.dTime == UNKNWOWN) myCal.dTime = dTime; } } }
PMD result:
The method usefullMethod() has an NPath complexity of 10625
If I add a new variable initialized the same way, I got this:
The method usefullMethod() has an NPath complexity of 103125
And if I replace all? With if-else structure, then I got this:
The method usefullMethod() has an NPath complexity of 1056
Why do I got this very high result with the ternary '?' Operator?
What's wrong with this code? (In this demo code it is easy to extract a method for getting the default values, but in real code it might not be possible)