DEV Community

Discussion on: Think twice before using a Supplier

Collapse
 
florianschaetz profile image
(((Florian Schätz)))

I believe one of the important things to keep in mind when using lambdas and streams is to keep it readable. This can be done by explicitly declaring functions, filters, etc. or to use method references instead of nesting the code in complex statements.

By putting the code into methods, for example, anyone reading the code can start at the easily understandable "top layer", getting the big picture view. With well named methods, they then can check how exactly does, for example, this::replacePlaceholders do its job - but for the big picture, it's just important to know "Ah, and now he's somehow replacing the placeholders".

Unless the statement is very easy, I tend to refactor it into a method reference most of the time (since defining functions in the method itself also makes the method bigger and more complex).