question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Function calls that take a single lambda should not be on a new line

See original GitHub issue

Overview

When there is a only a single argument in a function call it seems unnecessary to place it onto a new line. This misaligns with how other prettier languages work, like in Javascript:

A single argument taking a lambda

image

When there are multiple arguments, e.g. two lambdas

image

Steps to reproduce

Prettier-Java 0.8.0

Stream methods (map, filter, etc)

Input:

final List<Integer> values = Stream.of(1, 2)
    .map(n -> {
        // testing method
        return n * 2;
    })
    .collect(Collectors.toList());

Output:

final List<Integer> values = Stream
    .of(1, 2)
    .map(
        n -> {
            // testing method
            return n * 2;
        }
    )
    .collect(Collectors.toList());

Expected behavior:

final List<Integer> values = Stream
    .of(1, 2)
    .map(n -> {
        // testing method
        return n * 2;
     })
    .collect(Collectors.toList());

Completable Futures (single argument)

Input:

CompletableFuture.supplyAsync(() -> {
    // some processing
    return 2;
});

Output:

CompletableFuture.supplyAsync(
    () -> {
        // some processing
        return 2;
    }
);

Expected behavior:

CompletableFuture.supplyAsync(() -> {
    // some processing
    return 2;
});

Completable Future (Multiple Arguments)

Input:

CompletableFuture.supplyAsync(() -> {
       // some processing
       return 2;
}, executor);

Output:

CompletableFuture.supplyAsync(
    () -> {
         // some processing
         return 2;
    },
    executor
);

Expected behavior:

CompletableFuture.supplyAsync(
    () -> {
        // some processing
        return 2;
     },
     executor
);

Function arguments that exceed width

Input:

myFunction((one, two, three, four, five, six) -> {
       // some processing
       return 2;
});

Output:

myFunction(
      (one, two, three, four, five, six) -> {
           // some processing
           return 2;
      }
);

Expected behavior:

myFunction(
      (one, two, three, four, five, six) -> {
           // some processing
           return 2;
      }
);

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
clementdessoudecommented, Aug 17, 2021

If I’m not mistaken, I already had a look, and it was not so easy to fix, as the syntax tree is quite complex around lambdas. But it would definitely be better formatted on a single line. I’ll try to have a look soon, I may have more inspiration 😃

1reaction
jduboiscommented, Aug 21, 2021

OK, increasing the bounty to $300

Read more comments on GitHub >

github_iconTop Results From Across the Web

lambda function in different lines - Stack Overflow
I feel when I write it is ok to use lambda function in one line, is quick and good, but when I check...
Read more >
CS 61A Higher-Order Functions, Self Reference Fall 2021
Unlike def statements, lambda expressions can be used as an operator or an operand to a call expression. This is because they are...
Read more >
Best practices for working with AWS Lambda functions
This is especially important when your Lambda function makes network calls to resources that may not handle Lambda's scaling. Use most-restrictive permissions ...
Read more >
Best practices when you use Lambda expressions in Java
Braces and return statements are optional in one-line lambda bodies. This means, that they can be omitted for clarity and conciseness. Do this:...
Read more >
Lambda expressions and anonymous functions | Microsoft Learn
Use the lambda declaration operator => to separate the lambda's parameter ... The body of an expression lambda can consist of a method...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found