Add LambdaBodyLength

commit-id:01a58e06
This commit is contained in:
Paul Campbell 2021-08-11 18:00:29 +01:00
parent 035b45b06c
commit fd818f9dc6
2 changed files with 55 additions and 0 deletions

View file

@ -1552,3 +1552,10 @@ rules:
enabled: true enabled: true
source: CHECKSTYLE source: CHECKSTYLE
uri: https://checkstyle.sourceforge.io/config_javadoc.html#JavadocMissingLeadingAsterisk uri: https://checkstyle.sourceforge.io/config_javadoc.html#JavadocMissingLeadingAsterisk
-
name: LambdaBodyLength
parent: TREEWALKER
level: COMPLEXITY
enabled: true
source: CHECKSTYLE
uri: https://checkstyle.sourceforge.io/config_sizes.html#LambdaBodyLength

View file

@ -0,0 +1,48 @@
Checks lambda body length.
Rationale: Similar to anonymous inner classes, if lambda body becomes very long
it is hard to understand and to see the flow of the method where the lambda is
defined. Therefore, long lambda body should usually be extracted to method.
Valid:
````
Runnable r3 = () -> { // ok, 10 lines
System.out.println(2); // line 2 of lambda
System.out.println(3);
System.out.println(4);
System.out.println(5);
System.out.println(6);
System.out.println(7);
System.out.println(8);
System.out.println(9);
}; // line 10
````
Invalid:
````
Runnable r = () -> { // violation, 11 lines
System.out.println(2); // line 2 of lambda
System.out.println(3);
System.out.println(4);
System.out.println(5);
System.out.println(6);
System.out.println(7);
System.out.println(8);
System.out.println(9);
System.out.println(10);
}; // line 11
Runnable r2 = () -> // violation, 11 lines
"someString".concat("1") // line 1 of lambda
.concat("2")
.concat("3")
.concat("4")
.concat("5")
.concat("6")
.concat("7")
.concat("8")
.concat("9")
.concat("10")
.concat("11"); // line 11
````