Are We Sacrificing Readability for Conciseness in Modern Java?
Saami abbas Khan

Saami abbas Khan @saamiabbaskhan

About: Hello! I’m Saami, an aspiring coder currently pursuing a bachelor's degree in computer science. I’m passionate about tech.

Location:
India
Joined:
Sep 13, 2024

Are We Sacrificing Readability for Conciseness in Modern Java?

Publish Date: Sep 22 '24
4 5

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!

Comments 5 total

  • Prasad Saya
    Prasad SayaSep 23, 2024

    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.

  • Ashwin Kumar
    Ashwin KumarSep 23, 2024

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

  • Osborn Tyler
    Osborn TylerSep 24, 2024

    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.

    • Saami abbas Khan
      Saami abbas KhanSep 24, 2024

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

Add comment