Sponsored Link •
|
Summary
Ken Arnold just posted a blog entry describing how difficult he found generics when working on the upcoming edition of The Java Programming Language book.
Advertisement
|
The fact that it's taken this book so long to get published is just one indicator of the complexity of the language. I've also noticed that most other Java books have either taken a long time, or just briefly glossed over generics, and often both. I don't feel so bad about how long Thinking in Java 4e has taken, because I've been putting a great deal of effort -- and it has taken all of it -- to understand and cover the new features in the language. The generics chapter, even though I've already spent months on it, will be the last to get done and the chapter that will remain the hardest (possibly neck and neck with Concurrency).
One commenter to Ken's blog points out that it's not the idea of generics that is the problem, but the implementation, to which I agree, and I also think this is what Ken actually meant. I am very fond of the idea of generics/templates/parameterized types. They add a great deal of power to a language. That's why it was so frustrating to discover the limitations in the way it is implemented in Java. After a fair amount of back-and-forth I understand why the limitations are there. Heck, I practically agree with the idea of migration compatibility. Without something like that, too much would be broken (although you could probably argue that it would have been acceptable to break a bit more code rather than give in to erasure).
But I think the real question that Ken is posing is this: "if we had known from the beginning what generics would look like, would we have still said it was worth the cost?"
I periodically get dismissed when I suggest that generics are hard, by someone who hasn't delved into them very deeply but has instead been charmed by the thought of List<String>. How hard could that be? If you look at it simply, it seems all pluses and no minuses. The problem is that, despite what some of the promoters within Sun said, to the effect that everyone could just be a consumer of generic libraries so it would all be as simple as List<String>, you will want to write your own generics, and once you open that can of worms, it becomes far messier than you'd like to think.
Here's what I think is the best resource for Java Generic knowlege: Angelika Langer's Java Generics FAQ. Angelika is wicked smart, to the point where she thinks that a lot of things about both C++ Templates and Java Generics should be obvious (but not so obvious that she didn't write the FAQ), and even she occasionally has a hard time with this stuff.
As much as Java people like to dis C++ for just about everything, C++ templates are much more straightforward than Java generics, because they are consistent. With erasure, you have to constantly remind yourself that you only think you know what a particular parameterized type is. But because you don't really (except sometimes), it causes all kinds of confusion. And a programming language is supposed to be an abstraction to reduce confusion, so Ken's comment about complexity: "I don't see that the value is there to justify the cost," is legitimate.
Apparently Gosling had thoughts of including generics in Java 1.0, but someone had come up with a deadline and adding generics would have broken it (I would argue that the effort to add generics for J2SE5 was much greater than it would have been to have just fixed the language before releasing it in the first place). If they had put it in at the beginning, things would have been a lot different because it could have been consistent in the same way that C++ is consistent (and before you complain about templates, note that it's always been bad error messages that have made C++ templates difficult -- and this is being addressed in the next version of the language). But it wasn't and it isn't.
Despite all that, I kind of like generics now. At least, now that I've gotten over the formidable learning curve. Or mostly over it -- I think it will be something that I will always have to step back from and take a few deep breaths before diving back in. And note that the reason I like generics is not a rational one, but rather the nerd's satisfaction of having successfully tackled something that is capriciously complex, without giving up. But this is not generally a good thing, and I would rather spend my effort and energy producing something new and valuable rather than grappling with the deep-sea octopii of complexity.
As Ken points out, we are stuck with generics in their current form, even if one commenter hopefully suggested that everything could be substantially reworked for J2SE 7.0 (because of the pain of implementing generics the first time, and the pain that people will experience when adopting them, I think I can safely guess that this won't happen). I can live with them, and even do interesting things with them (as you shall see when Thinking in Java 4e finally comes out), but I'm starting to think that J2SE5 may represent the last of the big language changes to Java. For one thing, as soon as J2SE5 was officially released, a number of key compiler folks left Sun. But more importantly, I think that the ability for programmers to incorporate the new language features that have been appearing in Java may have reached a saturation point, at least for a few years to come. I can safely say that generics will slow them down.
Have an opinion? Readers have already posted 46 comments about this weblog entry. Why not add yours?
If you'd like to be notified whenever Bruce Eckel adds a new entry to his weblog, subscribe to his RSS feed.
Bruce Eckel (www.BruceEckel.com) provides development assistance in Python with user interfaces in Flex. He is the author of Thinking in Java (Prentice-Hall, 1998, 2nd Edition, 2000, 3rd Edition, 2003, 4th Edition, 2005), the Hands-On Java Seminar CD ROM (available on the Web site), Thinking in C++ (PH 1995; 2nd edition 2000, Volume 2 with Chuck Allison, 2003), C++ Inside & Out (Osborne/McGraw-Hill 1993), among others. He's given hundreds of presentations throughout the world, published over 150 articles in numerous magazines, was a founding member of the ANSI/ISO C++ committee and speaks regularly at conferences. |
Sponsored Links
|