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 McClean
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:
- How to Swap Java Reference Types in a Method
- Dump Java: Life Is Just Better in Python
- Comparing Java to Python: A Syntax Mapping
Otherwise, take care! See you next time.
Recent Posts
As the semester starts to wrap up, I'm a bit busy and a bit burnt out. As a result, I thought I'd have a little fun writing about nasty little bugs I've run into over time. I'll keep it short; don't...
I find the structure of relational databases to be appealing. They allow you to store data in various tables that eliminate redundancy, reduce storage size, and combine easily. Therefore, it should...