Skip to main content
Clarify by incorporating Jimmy James' comment as a good example.
Source Link
Karl Bielefeldt
  • 148.9k
  • 38
  • 285
  • 485

I don't write Java code anymore, but I write in a functional language for a living, and also support other teams who are learning functional programming. Lambdas have a delicate sweet spot of readability. The generally accepted style for them is you use them when you can inline them, but if you feel the need to assign it to a variable for use later, you should probably just define a method.

Yes, people assign lambdas to variables in tutorials all the time. Those are just tutorials to help give you a thorough understanding of how they work. They are not actually used that way in practice, except in relatively rare circumstances (like choosing between two lambdas using an if expression).

That means if your lambda is short enough to inline itbe easily readable after inlining, like the following is inlinedexample from @JimmyJames' comment, then go for it.:

people = sort(people, (a,b) -> b.age() - a.age());

Compare this with the readability when you try to inline your example lambda:

results.filter({result -> someOtherService.getResultDateFromDatabase(result).after(new Date())})...

Unfortunately, for thisFor your particular example, I'd personally flag it on a pull request and ask you to pull it out to a named method because of its length. Java is an annoyingly verbose language, so perhaps in time its own language-specific practices will differ from other languages, but until then, keep your lambdas short and inline.

I don't write Java code anymore, but I write in a functional language for a living, and also support other teams who are learning functional programming. Lambdas have a delicate sweet spot of readability. The generally accepted style for them is you use them when you can inline them, but if you feel the need to assign it to a variable for use later, you should probably just define a method.

Yes, people assign lambdas to variables in tutorials all the time. Those are just tutorials to help give you a thorough understanding of how they work. They are not actually used that way in practice, except in relatively rare circumstances (like choosing between two lambdas using an if expression).

That means if your lambda is short enough to inline it, like the following is inlined, then go for it.

results.filter({result -> someOtherService.getResultDateFromDatabase(result).after(new Date())})...

Unfortunately, for this particular example, I'd personally flag it on a pull request and ask you to pull it out to a named method because of its length. Java is an annoyingly verbose language, so perhaps in time its own language-specific practices will differ from other languages.

I don't write Java code anymore, but I write in a functional language for a living, and also support other teams who are learning functional programming. Lambdas have a delicate sweet spot of readability. The generally accepted style for them is you use them when you can inline them, but if you feel the need to assign it to a variable for use later, you should probably just define a method.

Yes, people assign lambdas to variables in tutorials all the time. Those are just tutorials to help give you a thorough understanding of how they work. They are not actually used that way in practice, except in relatively rare circumstances (like choosing between two lambdas using an if expression).

That means if your lambda is short enough to be easily readable after inlining, like the example from @JimmyJames' comment, then go for it:

people = sort(people, (a,b) -> b.age() - a.age());

Compare this with the readability when you try to inline your example lambda:

results.filter(result -> someOtherService.getResultDateFromDatabase(result).after(new Date()))...

For your particular example, I'd personally flag it on a pull request and ask you to pull it out to a named method because of its length. Java is an annoyingly verbose language, so perhaps in time its own language-specific practices will differ from other languages, but until then, keep your lambdas short and inline.

Source Link
Karl Bielefeldt
  • 148.9k
  • 38
  • 285
  • 485

I don't write Java code anymore, but I write in a functional language for a living, and also support other teams who are learning functional programming. Lambdas have a delicate sweet spot of readability. The generally accepted style for them is you use them when you can inline them, but if you feel the need to assign it to a variable for use later, you should probably just define a method.

Yes, people assign lambdas to variables in tutorials all the time. Those are just tutorials to help give you a thorough understanding of how they work. They are not actually used that way in practice, except in relatively rare circumstances (like choosing between two lambdas using an if expression).

That means if your lambda is short enough to inline it, like the following is inlined, then go for it.

results.filter({result -> someOtherService.getResultDateFromDatabase(result).after(new Date())})...

Unfortunately, for this particular example, I'd personally flag it on a pull request and ask you to pull it out to a named method because of its length. Java is an annoyingly verbose language, so perhaps in time its own language-specific practices will differ from other languages.