Affects PMD Version:
6.0.0 (and older ones I suppose)
Rule:
CyclomaticComplexity
Description:
Currently, PMD treats CFG's of lambdas that are declared but not called in a method as a part of the method's CFG which falsely increases a reported cyclomatic complexity values of such methods.
Code Sample demonstrating the issue:
Here is an example of a dummy method which is considered by PMD as having cyclomatic complexity = 12 while the expected value is below 10:
public void notSoComplex(int intInput)
{
boolean binary;
switch (intInput)
{
case 0:
binary = false;
break;
case 1:
binary = true;
break;
default:
throw new RuntimeException();
}
BiConsumer<Object, Object> lambda1 = (message, nme) ->
{
if (binary)
{
System.out.println(message);
}
else
{
System.out.println(nme);
}
};
BiConsumer<Integer, Integer> lambda2 = (nme, message) ->
{
if ((nme != 0) && (message != 0))
{
try
{
System.out.println(nme);
}
catch (IllegalArgumentException illegalArgumentException)
{
throw new RuntimeException();
}
if (binary)
{
System.out.println(nme);
}
else
{
System.out.println(nme);
if (nme != 1)
{
System.out.println(nme);
}
for (int i = 0; i < message; i++)
{
System.out.println(nme);
}
}
}
};
}
Running PMD through: [Gradle]
Hi, sorry for the delay. Thanks for the report. I'm ok with not counting in the CFG of local lambdas into the enclosing method. But they should be measured anyway, and reported separately if they have excessive Cyclo. The metrics framework doesn't consider lambdas yet though.
I see no real need to count lambdas when they are called. For one, it would be unfair to not count lambdas declared outside the method. Wdyt?
@oowekyala I think that lambas should be treated in the same way as methods: that is, separately and at the place of declaration.
Right, I'll try to fix that ASAP
You should check for isFindBoundary of the node before drilling down, check https://github.com/pmd/pmd/blob/master/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/AbstractNode.java#L285
Lambdas, nested classes, etc all return true for that method.
Most helpful comment
@oowekyala I think that lambas should be treated in the same way as methods: that is, separately and at the place of declaration.