Sunday, August 3, 2008

Java and the closures hall of shame

So, to see how far the closures rabbit hole goes I decided to make a list of all the languages in the top ten on the Tiobe index which have no closures support or no confirmed closures support in an upcoming version.

The initial list consists of these languages:
- Java.
- C.
- C++.
- PHP.

Oops I forgot about C++0x and it looks like it has closures support, remember I said confirmed closures support in an upcoming version.

Oh and lets not forget PHP 5.3 and the closures rfc that's already in HEAD.

Therefore the refined list looks as follows:
- Java.
- C.

Now what about C. It's true that ANSI C forbids nested functions but as it turns out GCC has a few extensions which allow you to do closures. It's also worth considering if C belongs here since it's modern use case and characteristics tends to place it in a different category to the rest of the languages on the index. On those grounds I'm going to apply Occam's razor and remove C from the list.

The final list looks as follows:
- Java.

This means that after the next iteration of language versions, Java will lack in capabilities compared to competing languages. Now I can carry on coding without closures - albeit somewhat unhappily. But bear in mind that Java increasingly has to compete with other languages. And if Java is perceived to be obsolete it's going to be a harder sell in the enterprise.

Now I know that time is running out for finalizing Java 7.0 but for goodness sakes Sun you really need to do something.

Now purely from my point of view I wouldn't be upset with either of the following:
- Pushing out the Java 7.0 time line in order to add these language features.
- Make another language such as Groovy a required part of the Java 7.0 spec.

8 comments:

Casper Bang said...

Sun is just too conservative for their own good. It's a shame really, they pushed some great features and practices into the mainstream back in the 90's. Why they do not acknowledge the growing entropy and say "Ok, time for a reboot of Java" is beyond me. Instead they decide to create a new language and try to arm wrestle Adobe and Microsoft in the RIA space. Go figure.

Ricky Clarkson said...

I'm not sure why you mentioned Groovy here - it's worse than Java. int i="hello"; is valid Groovy (no static typing).

Joshua Cranmer said...

There's one thing about closures that galls me. Almost every single post I've seen on it predicts that it's the ultimate feature of a language, something which evidence seems to fail to support.

I have used a language with closures: Javascript, and not the common-denominator ECMAScript people use in webpages either. And the result? Most of the time, I use the good old "for each (var part in parts)" or even "for (var i=0;i < parts.length; i++)". Sure, I have some filter's in there, maybe a map or two, and a handful of reduce's, but so much of what I need to loop cannot be handled with the functional versions, either because:

1. I have to access several contiguous variables at once,
2. I'm doing higher-order logic where the loop is affecting variables and CFG logic outside of it (and no, non-local returns would not solve that),
3. The loop is so complex that it is clearer to explicitly delineate it as such and not hide it in a function call, or
4. I'm modifying the array in such a way that the functions cannot handle.

In Java, do I miss the functions I use? Not really. Anything really common could become a static method, but all I'm doing is saying "this value is the number of elements where this is true" or something similar.

And, FWIW, these functions, as proposed for inclusion in Java, become totally worthless to me by omitting some useful parameters like the index of the element or whatnot.

Anonymous said...

A little while ago there was a blog post declaring Java dead because of the bloat of generics. Today Java is dead because it's not bloated enough.

Talden said...

@charleshope

I don't subscribe to that blogs view... Generics are poorly implemented - sure - absolutely - no argument from me. But IMHO they're still better than not having them.

My concern with leaving out closures is not 'will I use them in every method' but how java is perceived. Growth in enterprise requires a constant stream of new developers picking up the language - I'm skeptical that java can retain the interest of the new generation if it forces them to abandon programming paradigms born of features in languages they learnt during training.

Okay it's too late for Java 7 - sign off Java 7 now as a small jump over Java 6 and be sure to get these features into Java 8 with a short release cycle.

If Sun can't make it a short cycle I expect that soon businesses will start to pick new languages they can get developers for.

This all said - I want Closures because I do have uses for them now dammit - I also don't feel that closures lite will cut it...

Erik I said...

I like the Java language because it's really clean and has good debugging support.

I also do some work in Delphi, and one thing that strikes me is that if it is easy to make hard to read code, even really smart people will do that (e.g. writing multiple classes in one unit, with all class declarations on top and the implementations stacked away at the bottom. Or using the with statement to reduce typing.)

Rails left me impressed when I first saw it a couple of years ago. But closures is, in my opinion, not the important thing about ruby. The important thing about ruby is rails, and that's where I think the major problem with Java is also. Java lacks a good full stack web framework that does cool things within 5 minutes, and continues to deliver value over the next months, something like rails or symfony. (Yes, I know about grails, trailsframework and seam, but for some reason or another, when I'm not at work, I always choose php/symfony over Java. Because it just works. Because it takes less than 5 minutes to generate a project and get started. Because I don't have to restart the server process just because I changed a class. Because I can host it dirt cheap at almost any web host.)

Unknown said...

Or, you could look at the popularity of the "top 10" languages as a reflection of programmer's preference for simplicity.

Your list of 4 languages that don't have closures is:
- Java.
- C.
- C++.
- PHP
...and those are 4 of the top 5 most popular.

Your list of 2 (Java, C) and list of 1 (Java) match their popularity.

There are many language features which one or two languages in the top 10 dont support. That doesn't necessarily imply "obsolete" or "feature-deprived". It could be just "left out by design". That's certainly the case of closures and Java today.

Anonymous said...

What do you know Pirates of the Burning Sea Gold. And do you want to know? You can get potbs gold here. And welcome to our website, here you can play games, and you will get potbs Doubloon to play game. I know potbs money, and it is very interesting.Do you want a try, come and view our website, and you will learn how to buy potbs Doubloon. Come and join with us. We are waiting for your coming.