DEV Community

Saami abbas Khan
Saami abbas Khan

Posted on

Are We Sacrificing Readability for Conciseness in Modern Java?

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));


Enter fullscreen mode Exit fullscreen mode

I'd love to hear your thoughts!

Top comments (5)

Collapse
 
osborntyler2 profile image
Osborn Tyler

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.

Collapse
 
saamiabbaskhan profile image
Saami abbas Khan

Totally agree with you—thank you for sharing such a valuable perspective on balancing brevity and clarity! 🫡

Collapse
 
prsaya profile image
Prasad Saya

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.

Collapse
 
saamiabbaskhan profile image
Saami abbas Khan

Got it Thanks!

Collapse
 
ashwin_kumar_685de66d0e2b profile image
Ashwin Kumar

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