O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

There's more...

The following are the best practices for constructing a lambda expression:

  • Keep it stateless
  • Avoid a block statement
  • Use a method reference
  • Rely on effectively final
  • Do not work around effectively final
  • Avoid specifying the parameter type
  • Avoid brackets for a single parameter
  • Avoid braces and the return statement

An API designer should keep these guidelines in mind too because the API clients will probably use them.

Keeping lambda expressions stateless means that the result of the function evaluation must depend only on the input parameters, no matter how often the expression is evaluated or which parameters were used for the previous call. For example, this would be a bug-prone code:

GrandApi api = new GrandImpl();int[] ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required