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

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

BT