David Jones

The only thing I want in a "MashUp" is Potatoes with some Sausage and Beans!

My Consulting Company

Calendar

««Jul 2009»»
SMTWTFS
    1234
567891011
12131415161718
19202122232425
262728293031

My RSS Feeds








Search Box

 

Closure Headache

posted Tuesday, 12 August 2008

So I was listening to a presentation of proposed language features for Java 7 on infoq .  Of course the prospective closure feature came up.  There is a good article on javaworld that I have read a couple of times to try and understand the subject some more.

It does give a good basic summary of what a Closure is,

"Essentially, a closure is a block of code that can be passed as an argument to a function call, which will execute the block immediately or some time later. To some extent, a closure is similar to an unnamed (or anonymous) function. There is more to it, but for the time being, let us stick to this definition. "

So it does not seem that bad on the face of it.  My headache comes when I start thinking about adding another feature to a language that is already getting syntactically very complex.  Closures are not simple and if you read the article the fact that there are three competing specs at this moment shows how complex a subject it is. 

The classic argument of "well you do not have to use the feature" does not hold water with me.  Developers love to use new features even if they are a bad fit.  I have read  many times code to do some basic feature that is mind blowing in its complexity because someone wanted to use a language/framework feature they had just read about.

It does look like Java 7 may be spared Closures.  Because there are three specs and lots of arguments means probably it will not make it in.  Let's hope this is the case.  Otherwise I am looking forward to Closures becoming some weird interview question.

links: digg this    del.icio.us    technorati    reddit




1. Greg M left...
Tuesday, 12 August 2008 8:36 pm

Agreed. Better to let people who want modern language features to migrate to modern languages. Java is going to be a legacy platform for some time to come, do we really want to exacerbate things by encouraging people to write new Java code? Code that's not even compatible with the existing legacy? We don't need the complications.


2. jim left...
Wednesday, 13 August 2008 1:03 am

Dont drop Java there to much investment. C++ will soon be more modern than Java. If Java is not modern it is legacy and I will not use it for new stuff. I want closures, I already use them in other languages in my work it would not make my life more complex.


3. Patrick left...
Wednesday, 13 August 2008 3:29 am

Java is simpler than C++. So if your argument was correct Java would't stand a chance agains C++, especially in 1998 when Java didn't even have Generics. Being more complex/moderd is not a good thing, it's a bad thing. Except for the gurus of course, but thos are like 2% of the developers (but 70% of the bloggers)


4. Ignacio Coloma left...
Wednesday, 13 August 2008 5:17 am :: http://icoloma.blogspot.com

"the fact that there are three competing specs at this moment shows how complex a subject it is"

No, it shows that there are some competent people showing interest on this, and they didn't get to an agreement. It says nothing about the complexity of closures.

When applied the same rationale to interpreted language you get Ruby, JRuby and Groovy, and it's not because of inherent complexity, but because you think you can get a better, rounder wheel.

Me, I would like closures without having to resort to dynamic languages, which have some features I don't like. I could really live without the embedded XML, or reimplementing OSGi - thank you. But, having used closures in other languages, I really miss them in java.


5. David Jones left...
Wednesday, 13 August 2008 8:26 am

Wow obviously Closures is a hot topic. Especially if a small blog like mine gets 4 posts on the subject.

I agree having different ideas on how to implement something like Closures does show there is a lot of interest. However it also shows it is complex. Debate generally goes hand in hand with complexity.

I am still not sure what adding closures would really add to for the language. Generics gave us type safety, XML is there because a lot of Java programs use XML and OSGi is being looked at for better deployment and application loading.

Adding new features does not make a language modern. Especially if it does not fit the language profile. Java is still the number 1 language. It is simple, cross platform and the developers who use it tend to believe in things like Continuous Integration and good development practices.


6. Jesse Kuhnert left...
Wednesday, 13 August 2008 8:42 am :: http://blog.opencomponentry.com

@David

My might-be-wrong guess is that closures look complicated to people who have never used them. I won't bore you with analogies of how things we all know how to do but at first seemed complicated might be similar experiences to this.

I can guarantee you that having them in the language would have an overall effect of reducing the size of your code base. It lets you turn "boilerplate" in to re-usable chunks of code. Almost anything you might do with transactions / AOP / exception handling / etc. / etc are all good candidates for this kind of feature.


7. David Jones left...
Wednesday, 13 August 2008 10:22 am

Jesse

Do you have any examples or links of how Closures can help making sure you keep to the DRY principle?


8. Nathan Sanders left...
Friday, 15 August 2008 5:28 am :: http://www.sandersn.com/blog

Here is how closures help you keep DRY. It's not dramatically better than using an inner class because it doesn't really use the power of closure to close over non-final variables. But it's easier than writing an inner class.

Instead of writing each time

List<A> filtered = new ArrayList<A>();
for(A a : allitems) {
  if(a.isGood()) {
    filtered.add(a);
  }
}

With first-class functions, you can factor this into:

List<A> filter(Function<A,Boolean> f, List<A> l) {
  List<A> filtered = new ArrayList<A>();
  for(A a : l) {
    if(a.isGood()) {
      filtered.add(a);
    }
  }  
  return filtered;
}
/* and */
filter(isGood, allitems);
/* or */
filter(isCurrent, newsitems);

You're no longer repeating the series (1) new List (2) for (3) if (4) add. In systems with good OO/functional integration, you should be able to use first-class methods too, something like filter(A.isGood, allitems) or filter(_.isGood, allitems). You can do this with inner classes, of course, but the syntax is almost as long as the original and many times longer than just A.isGood. In fact, the best use case for closures in an OO language is not the twisty confusing modification of state. If you're going to do that, use a class! That's what they're for.

I should finish off with a disclaimer that I am not a Java programmer. I write Python and Lisp, where passing functions around is common. But I've recently had to write some Java for the first time in 4-5 years and despite how cool the changes sound from the outside, the language is worse for them, I think. Greg M is right: if you want modern features, use a modern language. Leave Java simple like it started out.


9. Edwin Martin left...
Wednesday, 27 August 2008 7:22 am :: http://www.bitstorm.org/edwin/en/

Closures are a bit hard to grasp initially, but once you know how they work, they are very powerful.

Last year I've done a lot of JavaScript programming and used closures to easily solve problems which would otherwise be quite complex. I know JavaScript is not Java, but a lot of the same problems apply, for example adding an event listener which has access to the object in which the event listener is defined.