BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Two years later, frustration with Generics continues

Two years later, frustration with Generics continues

Bookmarks

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.

When Nelson reaches this code:

public class BaseObjectAdapter < T extends BaseObject < T > > implements BaseObject < T > {
  /* various instance fields...*/
 private Reference canonicalReference; // with the usual getters and setters
}

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?

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • Thumbs up for use of generics.

    by Tomas Varaneckas,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Generics is a great feature, if you don't overuse it. Understanding how this works is definitely worth the cost.

  • Re: Thumbs up for use of generics.

    by Jason Carreira,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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.

  • Re: Thumbs up for use of generics.

    by Ricky Clarkson,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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?

  • Re: Thumbs up for use of generics.

    by Jason Carreira,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    I don't have any problem refactoring code... Hell, if I didn't care about refactoring code I'd just use Eclipse instead of IDEA ;-)

    Here's a fun challenge for you, create a hierarchy of interfaces, base classes, and subclasses that pass around type-specific Arrays as params and return types.

  • Re: Thumbs up for use of generics.

    by Taylor Gautier,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Agreed.

    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.

  • Re: Thumbs up for use of generics.

    by Mark Richards,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Generics is a great feature, if you don't overuse it.


    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.

  • Draft copy of Naftalin & Wadler's generics book available for download

    by Jim Bethancourt,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    A draft copy of Naftalin & Wadler's book (mentioned above) is available for download:

    java-generics-book.dev.java.net/files/documents...

    However, it looks like the amount of content has increased since the draft (posted in Oct. 2005) -- the page count went from 212 to 240, and some of the code examples may have been improved upon too.

    Cheers,
    Jim

  • Re: Thumbs up for use of generics.

    by Eric Smith,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    ...if you don't overuse it.


    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.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT