google / styleguide

Style guides for Google-originated open-source projects
https://google.github.io/styleguide/
Apache License 2.0
37.24k stars 13.29k forks source link

Document lambda formatting for Java 8 #112

Open GuiSim opened 8 years ago

GuiSim commented 8 years ago

Java 8 supports Lambda functions but they do not appear to be part of the documentation for the Java Styleguide.

Vladlis commented 7 years ago

The guide currently contains an example with Lambda: https://github.com/google/styleguide/blob/gh-pages/javaguide.html#L283-L287

return () -> {
  while (condition()) {
    method();
  }
};

But it is not enough to understand how to handle curly braces in the following code:

void foo1() {
        Stream.of("Hello").filter(s -> {
                return s != null;
            }
        ).collect(Collectors.toList());

        Stream.of("Hello").filter(s -> {
                return s != null;
        }).collect(Collectors.toList());

        Stream.of("Hello").filter(s -> {return s != null;})
                .collect(Collectors.toList());

        Stream.of("Hello").filter(s -> {return s != null;}).collect(Collectors.toList());

        Stream.of("Hello").filter(s -> {
            return s != null;}).collect(Collectors.toList());

        bar(() -> {return;}, () -> {return;});

        bar(() -> {
            return;
        }, () -> {return;});

        bar(() -> {
            return;
        }, () -> {
            return;
        });

        bar(() -> {
            return;}, () -> {return;});

        bar(() -> {
            return;
        }, () -> {
            return;});
    }

void bar(Runnable r1, Runnable r2) { }

We are about to finish adding Lambda to RightCurlyCheck in Checkstyle (https://github.com/checkstyle/checkstyle/issues/3546) and the next step would be adding it to google_checks config (https://github.com/checkstyle/checkstyle/issues/4178), so please provide clarification.