Two years later, frustration with Generics continues

Laird Nelson describes his frustrations with understanding Java Generics. While clear in the simple case, as he works through a more complicated scenario, he ends up throwing them away because they're so complicated.

In the two years since Java SE 5 was released, there have been many articles and tutorials posted about Generics. In the simple case (using generified collections) they appear to be well used. However, when digging deeper, developers easily get in over their heads. Issues such as self-bounding generics, wildcard types, or type erasure make generics harder to wrap your head around. A new book is out which might help developers, but at this point the question is if generics are just too complicated for the average developer, and they'll stick with 1.4 style casts.

he gives up, both because how ugly it is and because it is so complicated that even after writing it he cannot get his brain wrapped around it. One of the commenters to his post notes that when migrating to generics he would just try different combinations until the code compiled. If generics are not understandable by most developers, how much trouble are we getting ourselves into when using them?

Have you ever tried to design a reusable library using Generics? The value is questionable considering the amount of time and the number of changes you have to make to your code to generify it. And forget using Arrays, that's just a nightmare. The real fun is when you get a couple different lines of interfaces coming together in one implementation class and they both inherit back (at some level) to a common generic interface... getting the type qualifiers to match up is REAL fun.

Code is fluid - if you try to treat it as a solid object that requires real effort to manipulate, then you're disabling yourself.

I often change 5,000 lines of code a week, most of those through automated refactoring. I don't worry about the amount of change I need to make, any more than I worry about molecules of water crossing each others' path when I turn on the tap.

Generics is no different there. IDEs can help in generification too.

Can you think of an actual situation that's hard to get the type qualifiers to match up with?

I have found that Generics help me find where my design is flawed. If I can't genericize my code, it tends to show me where I've incorrectly placed dependencies, IOTW problems with Generics means tight coupling.

Very well stated, and I agree. My first priority from a coding and design perspective is simplicity. If Generics helps simplify my code, then I will use it. However, as Nelson pointed out through his code example, simplicity is *not* always equal to how concise or generic the code is.

When talking about Generics I always refer back to the J2EE design pattern days. Design patterns alone are ok, but not of much use. However, combine the various design patterns, and you have a great model. The same is with Generics and the other features of Java 1.5. Used alone, they don't seem all that useful. However, combine the features, and they can be quite powerful and significantly simply your code.

Compare with the C++ standard library. You know you've gone too far when you need to copy the type of an object out of the debugger watch window and paste it into an editor so you can format and indent all the templates just to figure out what the heck you've got.

InfoQ Weekly Newsletter

Join a community of over 250 K senior developers by signing up for our newsletter. If you are based in the EEA, please contact us so we can provide you with the protections afforded to you under EEA protection laws.

Is your profile up-to-date? Please take a moment to review and update.

Email Address

Note: If updating/changing your email, a validation request will be sent

Company name:

Keep current company name

Update Company name to:

Company role:

Keep current company role

Update company role to:

Company size:

Keep current company Size

Update company size to:

Country/Zone:

Keep current country/zone

Update country/zone to:

State/Province/Region:

Keep current state/province/region

Update state/province/region to:

Subscribe to our newsletter?

Subscribe to our architect newsletter?

Subscribe to our industry email notices?

By subscribing to this email, we may send you content based on your previous topic interests. See our privacy notice for details.

You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.

We notice you're using an ad blocker

We understand why you use ad blockers. However to keep InfoQ free we need your support. InfoQ will not provide your data to third parties without individual opt-in consent. We only work with advertisers relevant to our readers. Please consider whitelisting us.