Java Lambda Expressions Are a Scam

Java Lambda Expressions Are a Scam Featured Image

As a resident Java hater, I can’t help but go around making fun of this trash language. Today, I want to talk about Java’s famous lambda expressions and how aggressively mid they are.

Table of Contents

Passing Methods in Java is Clunky

As someone who teaches Java, I often find certain aspects of it unneccessarily complex. For example, every introductory Java class mentions iterators and comparators at some point. The concepts themselves aren’t too complicated. You use an iterator when you want to loop over a set of data, and you use a comparator when you want to sort some data.

Unfortunately, in practice, iterators and comparators are nightmares. For instance, if you’ve ever had the displeasure of wanting to sort something using a comparator, you know what I mean. For every type of sort you might want to conduct, you need to construct a new comparator. Don’t believe me? Let’s look at an example.

Think about having a collection of products, sort of like Amazon. There are a lot of ways that you might want to sort that data (e.g., price, relevance, release date, etc.). Now, in most modern programming languages, sorting that dataset is somewhat trivial. For example, here’s how I might sort that dataset by price using Python:

# define a list 'items' which contains a collection of store products
items = retrieve_products_list()

# sort items by price
items.sort(key=lambda item: item.price)

# sort items by date
items.sort(key=lambda item: item.date) 

On the other hand, in Java, we have to create a new Comparator for each type of sort we want to conduct:

// define an array 'items' which contains a collection of store products
Product[] items = RetrieveProductList();

// sort items by price
Arrays.sort(items, new Comparator<Product>() {
    @override
    public int compare(Product p1, Product p2) {
        return Integer.compare(p1.price(), p2.price());
    }
});

Not to mention that if we want to reverse the order—say highest price to lowest price—we have to write yet another comparator. I don’t know how else to explain how absurd this is.

Lambda Expressions “Save” the Day

As you can probably imagine, the reason Java is so broken is that it doesn’t allow you to pass around methods like most modern languages do. The lack of this core feature is what makes Java such a pain at times (ignoring all of its other issues like verbosity).

Fortunately, at some point along the way, Java introduced lambda expressions. You can probably imagine that this was quite good news given that Python sorting lets you actually pass methods around. Well, you’d be wrong. Java never fixed that issue. You still have to pass objects around with custom methods. Now, you just have a slightly nicer syntax to use:

// define an array 'items' which contains a collection of store products
Product[] items = RetrieveProductList();

// sort items by price
Arrays.sort(items, (p1, p2) -> Integer.compare(p1.price(), p2.price()));

Now to be fair to Java, they really filled out the Comparator class with useful methods to make sorting easier. That said, I don’t love the idea that additional methods need to be added to account for flaws in the underlying language. And if those additional methods don’t meet your needs, you’re going to be stuck rolling your own Comparator anyway. That’s not to mention that any function more complicated than above is going to require either multiple Comparators or a new class. That just seems like a nightmare compared to making a simple function. But don’t just ask me, here are what someone else said:

Lambda expressions in Java are simply a less verbose way of creating (slightly constrained) Objects and as such the most likely outcome of adopting Lambda’s without a good understanding of core functional concepts is gnarly, twisted, hard to follow, obfuscated imperative Object Oriented code with a nice concise syntax. Yes, we could write exactly the same dodgy code in a more verbose way by creating individual classes and lambdas.

John McCleanOpens in a new tab.

The Good News?

Ultimately, however, the article title was a bit of clickbait, so I could trash Java a bit. At the end of day, use whatever tools work for you. I’m sure there are plenty of wonderful uses for lambda expression since their addition. At the end of the day, I suppose my gripe is with the clunkiness of “method passing” in Java. If you do a bit more reading, you’ll find that method passing requires “utility classes,” which just seem like a mess themselves.

With all that said, I’m also generally of the opinion that programming languages should stick to a niche. Otherwise, you end up with endlessly bloated languages like C++. Java, itself, seems to be heading in that direction as well. By adding this weird retrofitted functional features, we end up with a truly messy language. Surely, there is a more OOP-style solution to the sorting problem.

At any rate, that’s about all I wanted to cover today. I’m currently in the middle of my candidacy exam, so I don’t have enough energy to have a more nuanced take. Hopefully, this won’t cause too much buzz, and I can go back to writing about fun stuff. In the meantime, why not check out one of these related articles:

Otherwise, take care! See you next time.

Coding Tangents (45 Articles)—Series Navigation

As a lifelong learner and aspiring teacher, I find that not all subjects carry the same weight. As a result, some topics can fall through the cracks due to time constraints or other commitments. Personally, I find these lost artifacts to be quite fun to discuss. That’s why I’ve decided to launch a whole series to do just that. Welcome to Coding Tangents, a collection of articles that tackle the edge case topics of software development.

In this series, I’ll be tackling topics that I feel many of my own students have been curious about but never really got the chance to explore. In many cases, these are subjects that I think deserve more exposure in the classroom. For instance, did you ever receive a formal explanation of access modifiers? How about package management? Version control?

In some cases, students are forced to learn these subjects on their own. Naturally, this forms a breeding ground for misconceptions which are made popular in online forums like Stack Overflow and Reddit. With this series, I’m hoping to get back to the basics where these subjects can be tackled in their entirety.

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife and kid, playing Overwatch 2, Lethal Company, and Baldur's Gate 3, reading manga, watching Penguins hockey, and traveling the world.

Recent Posts