From fd818f9dc670ca71168ba35916e1e4fbf621ec25 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 11 Aug 2021 18:00:29 +0100 Subject: [PATCH] Add LambdaBodyLength commit-id:01a58e06 --- builder/src/main/resources/application.yml | 7 +++ .../main/resources/rules/LambdaBodyLength.md | 48 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 builder/src/main/resources/rules/LambdaBodyLength.md diff --git a/builder/src/main/resources/application.yml b/builder/src/main/resources/application.yml index 7b17881..2a9c8be 100644 --- a/builder/src/main/resources/application.yml +++ b/builder/src/main/resources/application.yml @@ -1552,3 +1552,10 @@ rules: enabled: true source: CHECKSTYLE 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 diff --git a/builder/src/main/resources/rules/LambdaBodyLength.md b/builder/src/main/resources/rules/LambdaBodyLength.md new file mode 100644 index 0000000..a8b89d5 --- /dev/null +++ b/builder/src/main/resources/rules/LambdaBodyLength.md @@ -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 +````