What is the convention for variable names in lambda expressions?

would_like_to_be_anon

I see a lot of examples where for example

int sum = widgets.stream()
                  .filter(w -> w.getColor() == RED)
                  .mapToInt(w -> w.getWeight())
                  .sum();

can we use any variableName in these lambda expressions?

I thought variable names have conventions and is good to use proper names for readability.

For instance if I use w as Widget in pre-java8, the code would be shunned as not readable. What has changed with the advent of java 8?

   for(Widget w : widgets)
   {
         if(w.getColor() == RED) {
            sum += w.getWeight();
         }
    }

Why can't the code be written like this:

int sum = widgets.stream()
                  .filter(widget -> widget.getColor() == RED)
                  .mapToInt(widget -> widget.getWeight())
                  .sum();

Maybe the code above is doing something straight-forward, and only on widget in the list of widgets. So, something more:

Which is better readable:

    return  requestHolder.getRequests()
                        .stream()
                        .map(request -> request.getErrorHolder())
                        .flatMap(errorData -> errorData.getErrors().stream())
                        .collect(toList());

or

    return  requestHolder.getRequests()
                        .stream()
                        .map(t -> t.getErrorHolder())
                        .flatMap(r -> r.getErrors().stream())
                        .collect(toList());

Maybe I am missing something. Could you explain?

Jean Logeart

The tacit convention is: name your variables so the code is pleasant and easy to read.

In my opinion, a very short name or a dummy name is preferable when the scope of the variable is limited. It makes the code more readable, lighter and also indicates that the function of the variable is limited.

Explicit names are to be preferred when the variable is used extensively in the code and you want to avoid to have it confused with other variables.

for(int i = 0 ; i < max ; ++i) {
    int myExplicitVar = myArray[i];
    // rest of the code using myExplicitVar several times
}

For lambdas, the scope of the variable is usually extremely limited and I find it easier to read when the name is short so only the important parts of the code remains, which was the point of lambda expressions in the first place.

In your examples, I find the short names more straight to the point.

Short variable name makes the code less bloated but can lead to confusions in rich contexts.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What is the naming convention in Python for variable and function names?

From Dev

What's the convention for reusing variable names for both JSP pages and Java Servlets?

From Dev

What is the advantage of "lambda expressions"?

From Dev

Naming convention for exported module variable names

From Dev

Node.js convention for module require variable names

From Dev

Accepted convention for variable names clashing with keywords / standard libs

From Dev

What is the convention for @BeforeClass (@BeforeAll) and @AfterClass (@AfterAll) method names?

From Dev

Using variable names with regular expressions c++

From Dev

Using variable names with regular expressions c++

From Dev

Convert Lambda expressions that contains variable to string

From Dev

Lambda expressions "x cannot be resolved to variable" error

From Dev

C++ : What are the purpose of captures in lambda expressions?

From Dev

What does these variable names stand for?

From Dev

What type of variable that contain lambda

From Dev

What is the difference between "instance Variable Names" and "class Variable Names", in pharo?

From Dev

Naming convention for Python regular expressions?

From Dev

C# lambda expressions without variable / parameter declaration?

From Java

What characters are valid for JavaScript variable names?

From Dev

What are QtCreator build step variable names (such as %{buildDir})?

From Dev

What Are Valid Characters In Jenkins Variable Names?

From Dev

What characters are valid in environment variable names

From Java

Java 8 Lambda Expressions - what about multiple methods in nested class

From Dev

What's the reason for using lambda expressions to define functions in Scheme?

From Dev

What can be proved about parameters in lambda expressions, passed into monads?

From Dev

What is the OpenCV FindChessboardCorners convention?

From Dev

What is springboot versioning convention?

From Java

What is the variable between parenthesis and the arrow (->) in a Java lambda?

From Dev

What is the variable between parenthesis and the arrow (->) in a Java lambda?

From Dev

Is there a convention for what is a row and what is a column?

Related Related

  1. 1

    What is the naming convention in Python for variable and function names?

  2. 2

    What's the convention for reusing variable names for both JSP pages and Java Servlets?

  3. 3

    What is the advantage of "lambda expressions"?

  4. 4

    Naming convention for exported module variable names

  5. 5

    Node.js convention for module require variable names

  6. 6

    Accepted convention for variable names clashing with keywords / standard libs

  7. 7

    What is the convention for @BeforeClass (@BeforeAll) and @AfterClass (@AfterAll) method names?

  8. 8

    Using variable names with regular expressions c++

  9. 9

    Using variable names with regular expressions c++

  10. 10

    Convert Lambda expressions that contains variable to string

  11. 11

    Lambda expressions "x cannot be resolved to variable" error

  12. 12

    C++ : What are the purpose of captures in lambda expressions?

  13. 13

    What does these variable names stand for?

  14. 14

    What type of variable that contain lambda

  15. 15

    What is the difference between "instance Variable Names" and "class Variable Names", in pharo?

  16. 16

    Naming convention for Python regular expressions?

  17. 17

    C# lambda expressions without variable / parameter declaration?

  18. 18

    What characters are valid for JavaScript variable names?

  19. 19

    What are QtCreator build step variable names (such as %{buildDir})?

  20. 20

    What Are Valid Characters In Jenkins Variable Names?

  21. 21

    What characters are valid in environment variable names

  22. 22

    Java 8 Lambda Expressions - what about multiple methods in nested class

  23. 23

    What's the reason for using lambda expressions to define functions in Scheme?

  24. 24

    What can be proved about parameters in lambda expressions, passed into monads?

  25. 25

    What is the OpenCV FindChessboardCorners convention?

  26. 26

    What is springboot versioning convention?

  27. 27

    What is the variable between parenthesis and the arrow (->) in a Java lambda?

  28. 28

    What is the variable between parenthesis and the arrow (->) in a Java lambda?

  29. 29

    Is there a convention for what is a row and what is a column?

HotTag

Archive