Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the convention for variable names in lambda expressions? [closed]

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?

like image 490
would_like_to_be_anon Avatar asked Sep 08 '14 16:09

would_like_to_be_anon


People also ask

What is closure in lambda expression?

"A closure is a lambda expression paired with an environment that binds each of its free variables to a value. In Java, lambda expressions will be implemented by means of closures, so the two terms have come to be used interchangeably in the community."

What is the convention for naming variables?

The variable name must start with an alphabetic character, then it can consist of characters A-Z, 0-9, @, #, _ and the special characters £ and $.

Why do variables in lambda need to be final?

Forcing the variable to be final avoids giving the impression that incrementing start inside the lambda could actually modify the start method parameter.

Is closure the same as lambda?

Lambda functions may be implemented as closures, but they are not closures themselves. This really depends on the context in which you use your application and the environment. When you are creating a lambda function that uses non-local variables, it must be implemented as a closure.


2 Answers

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.

like image 141
Jean Logeart Avatar answered Sep 22 '22 12:09

Jean Logeart


Well I'm using Java8 for some time, and indeed I've found that I'm using longer names more often. My take on that is a simple rule of thumb: I like short name or not.

I usually go for short names like i or y if it's obvious what's happening inside of a stream. If you're processing a stream of widgets and doing a 4-6 operations, writing widget each time is annoying and lines start to look the same after couple of operations. So for stream of same things, where from context I easily know what I'm working on, I sometimes go for short variables. It's same as with the loop iterators, where everyone use i and not index.

On the other hand, if Im doing a lot of processing and especially if I need to map something, I like to give descriptive names, going even to extreme like widget, filteredWidget, widgetHeight etc.

As for common notation on the web, please remember that a lot of people try to promote new Java syntax. It's cool that it can be short so they tend to take it as far as possible. In Javaland we always had to write a lot, and my guess is that people overreact to the fact we can type less. Look for examples at Venkat S. talks about groovy or .js from 2 years back, he often undelrined how important it is that he does not have to type semicolon at the end of the line. That how our brain works I guess.

like image 23
Michal Gruca Avatar answered Sep 19 '22 12:09

Michal Gruca