I have been figuring my way into Groovy lately (JSR 241). And one thing that came accross my curiosity is Closures.

Closures are like pointers to a function that you can pass as a parameter. Yup, you read it right, pass the function as a parameter to another function. Other languages does have Closures (using the same name or another). We C# delegates, Groovy closures, and C function pointers.

Perhaps due to my orientation and upbringing as Java-centric developer without ever experiencing Smalltalk, the use and significance of Closures will never be clear to me.

It seems that the general trend in the Java language nowadays is to improve the language. Yes, some of them were very useful in pushing Java forward (when I say Java, I’m referring to the language, not the platform), some are plain useless.

These tuti-fruity enhancements to the language, are, I believe is in response to the relative ease of programming in other development paradigms. Say, .Net, LAMP, or perhaps, RoR. Any old-school Java developers will code and prefer the semantics of the older-Java. So, going back to Closures in Java. There is a silent-but-raging debate whether to include Closures in JDK 7 (and if indeed included, what will be its syntax). Though I have not seen much opposition to it in the web, I would still blurt out my unsolicited view on Closures.

Take the real world. Take the real business cases for example. I don’t think, there isn’t any large or realistic need for it. If any, these can be easily addressed through generalizations. And in the most extreme, inner classes, particularly, anonymous inner classes. Now that would be in extreme. In the real world, were much of the business is produced by the EE world, do you really require closures in your EJB? In Servlets? Or perhaps in you JSPs. How about JDBC? JNI? Can’t really think of any…

I believe that Closures will just be a language-level sweetie, and hence, not really needed at all. Java is statically-typed, OO-driven (yeah, debate me on this) language. Having said this, how would you model Closures? How will you document this? How about exceptions management? Closures will almost certainly ran afoul with your nicely-designed exception routines. What happens to EJB/JTS? What happens to your simple Collections? There is one heck of a large mindset to overcome if we are to support Closures.

You see, Java is everywhere. The cult-status following of Java simply makes it harder to implement new niceties. Yes, new features may be introduced but what percentage of the community uses these. Why not just build a new Language instead of further complicating the existing one. Think of it as JRuby, Groovy or perhaps Jython.

…Java is now open-source. Speaking of just creating a new language from scratch and putting all these from-other-language-envy features, do I smell a fork here?

My simple take on Java Closures: We don’t need Closures in Java. 10 years of SE and the robustness of EE is a sound testament to this. Java is fine, as it is.

And yes, I can survive Groovy too with ever resorting to Closures.