Hey everyone!
I’ve been working with Java’s lambda expressions lately, and something has been bothering me: are we sacrificing readability for the sake of conciseness?
I often see developers cramming everything into a single line, especially when using lambdas, streams, and method references. Sure, it looks clean and concise, but sometimes it’s just too hard to immediately figure out what the code is doing. For example:
names.stream()
.filter(name -> name.length() > 3)
.map(name -> new StringBuilder(name).reverse().toString().toUpperCase())
.distinct()
.sorted((a, b) -> b.compareTo(a))
.forEach(name -> System.out.println("Processed: " + name));
Top comments (5)
Hi! I understand your concerns about code readability when using lambda expressions. As a developer with experience at Softellar, I can say that brevity does not always mean better implementation. Sometimes, in pursuit of brevity, we can lose important details, which makes the code difficult to understand.
It is important to find a balance between brevity and clarity. Using lambdas and streams can indeed simplify some tasks, but if the code becomes illegible, it can reduce the productivity of the team. I try to follow the principle: "write code so that not only you, but also someone else can understand it".
My advice is to break complex lambda expressions into simpler steps and add comments where necessary. This way you will maintain readability and at the same time take advantage of the modern approach to development.
Totally agree with you—thank you for sharing such a valuable perspective on balancing brevity and clarity! 🫡
Yes, it is readable, clean, concise, expressive and also explains what it does clearly. I think you should use lambda expressions (and method reference expressions), when possible. Most of the commonly used Java APIs (file IO, collections, etc.,) now have APIs to use lambdas.
This also means using functional programming. Now you have object-oriented and functional programming capabilities to apply in your application development.
Got it Thanks!
Depends on how much we have used lamda functions, just have to get familar with the freequently used lamda functions and the code becomes readable too....