Statement lambda can be replaced with expression lambda
Asked Answered
H

4

84

I do user and invitation validation using the Optional facility

@DeleteMapping("/friends/{username}")
public
HttpEntity<Boolean> removeFriend(
        @ApiParam(value = "The user's name", required = true) @PathVariable String username
) {
    Long fromId = authorizationService.getUserId();

    return userService.findByUsername(username)
            .map(user -> {
                 return friendshipService.findFriendship(fromId, user.getId())
                        .map(friendship -> {
                            friendshipService.removeFriendship(friendship);

                            friendship.setToId(friendship.getFromId());
                            friendship.setFromId(friendship.getToId());

                            friendshipService.removeFriendship(friendship);

                            return ResponseEntity.ok(true);
                        }).orElseGet(() -> ResponseEntity.notFound().build());
            }).orElseThrow(() -> new ResourceNotFoundException("User not found"));

However, IntelliJ is colouring my grey return, But when I remove the return, it highlights to me that there is no return.

Could someone explain how it works and what is it all about?

Hysterical answered 15/9, 2017 at 11:39 Comment(0)
E
176

Your statement lambda

param -> { return expression; }

can be changed to an expression lambda:

param -> expression

Simple, isn't it? Note, that the curly brackets and the semicolon need to be removed.

Ela answered 15/9, 2017 at 11:46 Comment(3)
Works. Thanks. Which in your opinion is a better way to lambda this pastebin.com/imEtjwHp or this pastebin.com/gcaUMYQ4?Hysterical
There is no better in this context. Do it as you like. But for me, both code snippets are not readable. This should always be the first criterion for coding.Ela
@Ela would you mind giving us an example for the readable code snippets?Jaguar
O
5

Sometimes I found useful to leave the braces where they are if the block of code is long enough (I think it improves readability)

In Android Studio you can locally disable the warning using //noinspection CodeBlock2Expr at the start of the method like in the example below

//noinspection CodeBlock2Expr
button.setOnClickListener((View v) -> {
        //a long single method call...
});
Odd answered 13/11, 2018 at 8:31 Comment(0)
L
0

The Statement lambda can be replaced with expression lambda message appears when we use curly braces "{}" and semicolons ";" in expression lambdas for implementing functional interfaces. We can do away with those in such a situation. That being said, I personally feel it is a good coding practice to use block lambdas even for single line functional interface implementations, the way we do it for single line for and while loops.

Liquidator answered 30/12, 2020 at 13:19 Comment(0)
F
0

Reports lambda expressions with code block bodies when expression-style bodies can be used instead. The result of the conversion is shorter and more clear.

Example:

Comparable<String> c = o -> {return 0;};

After the quick-fix is applied:

Comparable<String> c = o -> 0;
Fusion answered 14/3, 2023 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.