Generics, Cheeta and complexity...
On the Eclipse 3.0 JDT mailing list there has been a lot of talk about the new JDK 1.5 features. They are working hard at adding generic support (along with other 1.5 features and things are coming along. But something struck me again today about how much I dislike generic types. It has been a long time since I've done any serious C++ work but without a doubt generics was something that made normal peoples brain hurt. So here we are about to have this lovely feature delivered to us and I find myself not wanting it.
I mean really how much more complex is this bit...
List myList = new ArrayList();
...
Iterator itr = myList.iterator();
while(itr.hasNext()) {
String foo = (String)itr.next();
...
}
than this bit...
List<String> myList = new ArrayList<String>();
...
Iterator<String> itr = myList.iterator();
while(itr.hasNext()) {
String foo = itr.next();
...
}
In reality most developers who don't understand the cast will not understand the 'generic type' stuff either and will just copy and paste the code and modify where necessary. I know we get a bit of additional type 'saftey' from the compiler here but once you get a ClassCastException once and figure out what caused it you tend to not make that mistake again.
My hope is that we will, as an industry & community, develop some 'best practices' that are borrowed from our C++ colleagues. I guess I don't really expect this to happen, I'm just hopeful.
Anyway this is a recent email from the list with an example of what has become possible in Cheeta. BTW: I love the little animated gif! Even if you are not interested in generics you should check out the gif :-).
Basic support for generic methods is now in, with argument type inference.
For instance, in the example below, U is inferred to be of type String for
the invocation of #foo(...) in #main(...) method.
public class X <T> {
public static void main(String[] args) {
new X<String>().foo(args, new X<X<String>>());
}
<U> void foo(U[] us, X<X<U>> xxu) {
System.out.println("SUCCESS");
}
}
We still need to test it a bit before updating the Cheetah with it.
So what I expect will happen is over the next year or two everyone will make all the same mistakes that have been made in the past with generic types in the C++ world. Here is an example of the kind of ugly stuff that we perhaps will be foisting upon ourselves. Not that meta-programming will be possible with Java generics but equally krufty stuff will be possible I'm sure. Just look at the above example. Try to explain that to the average 'business programmer' that the Simplify Java and J2EE crowd is aiming at.
me in a class with 15 'business programmers'
Well you see John the U stands for a generic return type from the method foo that is only specified when the method is invoked. So since the method foo is invoked with a type of X<X<String>> U is naturally a String. Get it?
John looking back with blank stare... Uh yeah sure so if I add two cups of sugar everything will be ok right?
I know I'm sort of too little too late here but my hope is that we as a community will learn from all the bad stuff that has been done in C++ with generics and not repeat the mistakes made there...
[Update:] Thanks to Ben's comment I figured I'd post the JSR homepage and a mail address for feed back. Let the expert group know your opinion.
[Update 2:] If only I'd read for myself and not rely on others comments... As David Flanagan points out in his blog the time for comment is long since passed so don't hassle the expert group :-)


You're not alone; a lot of people are second-guessing the value of generics when compared to the new complexity it introduces to the language.
Some say, "You don't have to use it," to which you've pointed out the obvious retort: "Ah, but I will have to read it."
My point is -- it's not too late to prevent this added complexity. Make your thoughts known to the appropriate JSR in the JCP. If you and enough others complain, things will change.
Posted by Ben Galbraith on April 15, 2004 at 06:28 AM MDT #
I was fiddling with the referrer information on my weblog that other day and found a wanted to use a method that returned a List. I wouldn't want to even have to look at the (natural language) docs to be told in no uncertain terms what it contained. It's all about documentation, not ClassCastException.
Posted by Tom Hawtin on April 15, 2004 at 12:46 PM MDT #
If you're unsure about generics, don't create new generic types. Doing that right is hard. Although they are much, much easier to understand if you give them real names instead of X!
As Ben said in the comments, you can also just not use generics at all. This, however, will cause the compiler to complain *a lot*. Every time you use a collection class without appropriate type parameters, the compiler will bug you about an "unchecked" warning. If you want your code to compile cleanly, you'll have to use generics with every API that has been converted to generics. This includes collections, reflection, and the new concurrency utilities.
Commenter Ben is incorrect when he says that you should make your thoughts known to the JSR. That time has come and gone. In the case of generics, the public review closed over a year ago! Things have been tweaked a lot since then, but the basic outline of generics in Java is fixed and unchangable at this point.
I have not used templates in C++, but I believe that generics in Java are less confusing. It takes some getting used to, and I was initially a skeptic, but generic types are something that you can come to love. When they work well, the syntax is really elegant and natural. Consider List<String>, Map<String,Integer>, Future<BigInteger>, and Comparable<Number>. The meanings of these types and their type parameters is pretty self-evident, and brings lots of expressive power to the language.
[Arrgh! HTML Syntax is disabled and I can't even use line breaks to format my comment into paragraphs! I suppose I should have previewed before writing so much. Arrgh!]
Posted by David Flanagan on April 16, 2004 at 08:50 AM MDT #
Hi David. I keep meaning to turn on html in the comments... I'll try to get to that today... Thanks for the comments. I think I get it, I'm just not sure I like it. We will have to understand them to use Java because as you said lots of the JDK has been converted to use them. I like the collection API with generics. It's the proliferation of this feature into IT shop projects that scares me. Specifically when I'm called in to help clean it up... The example that came through the eclipse JDT list (quoted above) is a great example. What are the average VB Joe guy going to do with that? Probably not much. That is my only point. We are going to end up with lots of nasty code (just like we had in C++ with multiple inheritance). I guess the other point is to encourage debate and foster education so that people that use generics will actually have thought it through before applying it. Thanks again! -bd-
Posted by Bill Dudney on April 16, 2004 at 10:50 AM MDT #