Closures for Java. Is it Really a Good Idea?

DZone 's Guide to

Closures for Java. Is it Really a Good Idea?

· Java Zone ·
Free Resource

There's a very good article on JavaWorld by Klaus Kreft and Angelika Langer (the latter is the author of the well-known Java Generics FAQ) with the title Understanding the closures debate. I heard about this article on JavaPosse episode 194, and it's also being discussed on Artima.

I knew that there were proposals to add closures to Java, but I hadn't yet looked into the details of the proposals. The article explains very clearly what closures are exactly and gives a good overview of the three main proposals that there are today: CICE + ARM, BGGA and FCM + JCA. Read the article if you want to know what the alphabet soup means exactly. In short:


  • CICE + ARM, proposed by Joshua Bloch and others, is not much more than a new, shorter syntax for anonymous inner classes.
  • BGGA, proposed by Gilad Bracha, Neal Gafter, James Gosling, and Peter von der Ahé, is the most ambitious proposal and almost makes Java a functional programming language.
  • FCM, proposed by Stephen Colebourne and Stefan Schulz, also adds functional programming paradigms but doesn't go as far as BGGA in some aspects.


What's so controversial about adding closures to Java? Adding any new feature to the programming language makes it more complex. There are people who fear that adding a powerful but complicated feature such as closures might make Java too complex - which means that people will turn away from Java in favour of easier programming languages. This talk by Joshua Bloch from JavaPolis 2007 makes clear how complicated Java could become if BGGA closures would be added.

So, the question is, is it really such as good idea to add closures to the Java programming language? I'm not sure that it is worth the trouble. After reading the article and seeing Bloch's talk, I'm convinced that adding far-reaching closures such as BGGA and FCM will not be so great. What I especially don't like in the BGGA proposal are the non-local returns: a return statement in a closure means you're returning from the method that the closure is called in, not from the closure itself. This makes code very confusing and hard to understand.

I can see how CICE closures would be useful and at least it looks like that proposal is a lot simpler than the other two. But regardless of how much complexity closures would add to Java, there are reasons for me to doubt whether it's a good idea at all to add closures to Java.

Java is an imperative programming language. Because of this, Java programmers generally use an imperative programming style. For example, we write a loop like this

List<String> names = ...;

for (String name : names) {

Adding closures will encourage a functional programming style, which is completely different from the imperative style. In the functional style, you'd write a loop very differently; you'd call a forEach method on the collection, passing it a closure to execute on each element of the collection (this is what it would look like with BGGA closures):

List<String> names = ...;

names.forEach({ String name => System.out.println(name); }); 

What are the consequences of this? Does this mean that for example there are going to be lots of new methods (such as the forEach() method shown above) in Java's collections API to support this style of programming? That would bloat the collections API. Can you imagine what a mess you would get if you're working on a project with multiple developers where some developers prefer the imperative style and others the functional style?

I'm not opposed to the concept of closures itself, they work very well in other programming languages such as Ruby. The difference is, however, that Ruby has had closures since the beginning, so that Ruby's standard library was designed to work with closures from the start. If (part of) the standard Java library, such as the collections API, which was designed for the imperative programming style, is going to be extended to add support for closures, it will just grow into a complicated and bloated piece of patchwork.

The cost of adding a new feature to the programming language is very high. Millions of Java developers around the world will need to invest time and money to learn the new feature. The authors of the thousands of libraries and frameworks will need to think about the consequences for their products. And closures is not just another minor feature, it will add a whole new programming paradigm to Java. Are closures really worth all that effort? So far, I haven't heard any convincing reason why it would be a good idea to add closures to Java. Sure, they are a cool and powerful feature. But how are closures going to make things easier for me as a Java programmer? I'm not convinced that they are worth the extra complexity and API bloat that they will add to Java.

(Originally posted on Blogging about Software Development)


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}