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 ...