DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Redefining Java Object Equality
  • Java vs. Scala: Comparative Analysis for Backend Development in Fintech
  • Singleton: 6 Ways To Write and Use in Java Programming
  • How To Get Started With New Pattern Matching in Java 21

Trending

  • Contextual AI Integration for Agile Product Teams
  • How to Format Articles for DZone
  • Unlocking Data with Language: Real-World Applications of Text-to-SQL Interfaces
  • Blue Skies Ahead: An AI Case Study on LLM Use for a Graph Theory Related Application
  1. DZone
  2. Coding
  3. Java
  4. Using Java 8? Please Avoid Functional Vomit

Using Java 8? Please Avoid Functional Vomit

Most developers suck. I think everyone can acknowledge there is a significant portion of the developer community in any language (but particularly Java) that just doesn’t know how to write good code.

By 
Sam Atkinson user avatar
Sam Atkinson
·
Jun. 06, 16 · Opinion
Likes (133)
Comment
Save
Tweet
Share
59.2K Views

Join the DZone community and get the full member experience.

Join For Free

As the old adage goes, with great power comes great responsibility. The advancements of Java 8 have provided us that power. It’s brilliant! We have true functional syntax in Java which allows us to write beautiful code in a much more terse fashion. We can perform functions in a parallel fashion using streams. We Java developers have finally entered the 21st century.

The thing is, most developers suck. Obviously this is a heavily loaded statement, and fundamentally what I think is good code is different to what you think good code is, but I think everyone can acknowledge there is a significant portion of the developer community in any language (but particularly Java) that just doesn’t know how to write good code. The introduction of the functional programming paradigm into Java 8 is in many cases like giving a Ferrari to someone who’s only just gotten their training wheels off (more cheesy analogies to follow).

I can’t even claim to be the first person to have had this fear. If you wanted to perform functional programming pre-Java 8 the only real option was Google Guava, a wonderful library that did it’s best to make up for some of Java’s greatest shortfalls via the medium of functional programming.

On the Guava wiki they have this beautiful line which has always stuck with me:

Excessive use of Guava’s functional programming idioms can lead to verbose, confusing, unreadable and inefficient code. .. when you go to preposterous lengths to make your code “a one-line”, the Guava team weeps.

In my experience and much to my chagrin, few people headed this advice. This is great advice and absolutely applies to Java 8. One of the greatest features of Java 8 is that it can reduce the verbosity; anonymous functions are a great example of this (taken from my video course).

return findBy(new Predicate() {
            public boolean matches(Movie movie) {
                return movie.title().toUpperCase().contains(partialTitle.toUpperCase());
            }
        });

        …becomes…

    return findBy(movie ->   movie.title().toUpperCase().contains(partialTitle.toUpperCase()));

That’s great. The code is now clearer and easier to understand. What a lot of developers don’t seem to understand is that the goal of functional programming is not just to reduce verbosity. Verbosity is not the problem.

Yes, it takes more physical characters in Java than in other languages to achieve things. But honestly, it doesn’t matter. We have great IDE’s like IntelliJ (and sure, Eclipse, but IntelliJ is better. I won’t even mention Netbeans) which do all the hard work for you. I can go just as fast in Java with my IDE as you can in any other language.

Which is why I’m going to say something controversial and against the grain and is going to make everyone angry in the comments. Java’s verbosity is, in most cases, a good thing.

Our goal when programming is not to produce the least amount of code possible, but to produce performant systems that are easy to maintain. An easy to maintain system makes it painfully obvious what all the code does. This can, and often does, mean intentionally writing extra code to make it really really clear what’s going on.

//original, less clear code
if(barrier.value() > LIMIT && barrier.value() > 0){
//extracted out to helper function. More code, more clear
if(barrierHasPositiveLimitBreach()){

Oh no! Extra code in an extracted method! But it’s a lot easier for the person reading the code to understand what’s going on, and it’s much less likely they’re going to destroy the code by misunderstanding what it’s doing.

Extra code is not the enemy

By this virtue, do not use functional program as some brilliant way to turn all your code into one-liners that are impossible to understand. If that’s your jam, try something like JS1K or one use Perl. Everyone else hates your code.

Let’s take this example from “10 Java one-liners to impress your friends”.  on how to print the song Happy Birthday.

range(1, 5).boxed().map(i -> { out.print("Happy Birthday "); if (i == 3) return "dear NAME"; else return "to You"; }).forEach(out::println);

For me, this code would be much, much easier in the Java 7 style:

for(int i = 1; i <=5; i++){
System.out.println("Happy Birthday " + (i == 3 ? "dear NAME" : "to you"));
}

Write beautiful code. Write easy-to-understand code. Do not make me and your co-workers weep. Use Java 8 functionality and one-liners for good, not just to save characters. Characters are cheap.

Java (programming language) code style Functional programming

Opinions expressed by DZone contributors are their own.

Related

  • Redefining Java Object Equality
  • Java vs. Scala: Comparative Analysis for Backend Development in Fintech
  • Singleton: 6 Ways To Write and Use in Java Programming
  • How To Get Started With New Pattern Matching in Java 21

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!