WARNING: This blog entry was imported from my old blog on blogs.sun.com (which used different blogging software), so formatting and links may not be correct.
I just came across this blog entry by
Ken Arnold (who's writing a book on the Java language) where the author argues that generics should not have been included in JDK 5.0:
I don't know how to ease into this gently. So I'll just spit it out.
Generics are a mistake.
This is not a problem based on technical disagreements. It's a fundamental language design problem.
and then the concluding remarks:
... it feels like the JSR process ran far ahead, without a step back to ask “Is this feature really necessary”. It seemed to just be understood that it was necessary.
It was understood wrong.
I disagree with Ken. The big point of adding generics is allowing people to use type checked collections and so on. Nearly all Java programmers use the Collections API. Using the genericized collections in JDK 5.0 is not hard - it's really easy, makes the code cleaner, and detects programmer errors. The fact that creating generic collections classes is hard is besides the point - most programmers don't have to. Yes, there's a price for having more flexible libraries: they're harder to write. That is nothing new. Developers can continue to write JDK 1.4-style APIs and libraries if they want to.
Perhaps the comparison is a bit unfair, but take EJBs. Prior to EJB3, developers had to work really hard to write an EJB. With EJB3, all they have to do is add some metadata annotations to their simple Java code, and everything else is handled automatically. Did this make writing an app server harder? You bet. But that's a fair trade-off - there will be a lot more programmers using EJBs than programmers writing app servers. While not a perfect analogy, I think the same principles apply here. For a few library designers, who now want to create generic types (they still have the option of creating old style types), life just got more complicated. However, for all their clients, life got easier. Easier is good.
You might want to look at the Generics FAQ by Angelika Langer:
ReplyDeletehttp://www.langer.camelot.de/GenericsFAQ/JavaGenericsFAQ.html
to see all the little problems that can arise with Java Generics.
Sure, doing a List<Foo> foo; is simple, but as soon as you want to accept a collection object as parameter, you'll have to learn about Wildcards & Co, because, for instance, List<Object> doesn't include List<String> and so on...
Oh, and you'll have to change from Generics user to Generics programmer very fast, for instance if you want to implement Comparable or Future,... or something like that, and I wouldn't say that only hardcore programmers do that.
Having said all that, Generics are mostly useful for readability... especially for Maps where the types of Key and Value are now easily visible (and you don't have to guess them,which often annoys the hell out of me, as many devs don't bother documenting the types...). The language complexity has had a major increase, and we'll see if that was good or not...
C++ templates are very different from Java generics. They are also more complicated and more difficult to use and debug :)
ReplyDeleteSorry, thought the name was above the post, I guess my comments was actually for Jeff Kesselman.
ReplyDelete