Generics Considered Harmful

Ken Arnold posted about the problems with generics, namely that they substantially increase code complexity without providing commensurate benefits.

He’s spot on – generics are the programming language feature that never should have happened, and I speak as someone who, when I was a heavy user of C++, loved generics. Even now I yearn for the elegant consistency of the Standard Template Library. But the points Ken makes from a Java perspective apply equally well to C++, and C# as well. That generics increase both the semantic and syntactic complexity of code is undeniable. Their benefit as a runtime type-checking mechanism does prevent – or at least help diagnose – certain classes of bug. But these bugs are not generally especially common, nor difficult to diagnose or fix in the first place. This can be seen quite clearly when considering that dynamic languages, which enforce no type checking at all, do not suffer in development effort nor code maintainability as a result – if anything, they demonstrate the converse.

One benefit that Ken doesn’t mention is the use of generics to signal a programmer’s intent – to make a collection homogeneous is, to a limited extent, to make it self-documenting. Again though, this benefit is small compared to the complexity costs.

When I started typing I thought I had more of a point. Ho hum.

7 thoughts on “Generics Considered Harmful

  1. Fair enough Barrett. You’re definitely right about a few things: My experience with generics in C++ does use type erasure like Java, which lacks several groovy features of the .NET implementation you describe (good link – that taught me lots I didn’t know.)

    I agree with you that, used sparingly, they can be handy, and they do certainly have a great intuitive appeal, especially for static-typed languages. I have a vague worry though, that the intuitive appeal might just be a psychologically indulgent promise of control, which sober analysis reveals doesn’t actually help very much.

    I fear that ‘sparing use’ might be tricky sometimes to keep on top of. It got to the stage in the C++ world where in many circles it felt like you weren’t really writing good code unless you’d templated the hell out of everything. They creep insidiously, always with best intentions, but sooner or later you look around to find yourself knee-deep in syntactic soup, templates of templates everywhere. I guess, like you point out, like a lot of over-used language features, this is down to discipline: personal, organisational and community-wide.

  2. While it doesn’t necessarily counter any of his/your arguments, it is worth noting that generics in C# are implemented differently than in Java and C++. Whereas in Java (and I believe C++) the compiler expands the generic at compile time into wrapping methods (and loses track of the underlying type the generic is for), the .NET Framework actually produces a unique type for each generic. Additionally, C# supports generics with value types, which Java doesn’t support.

    From my own limited experience, while they can certainly add to complexity, if used sparingly (ie mainly for collections), they seem to be an excellent tool. At least, it feels much more natural and efficient to me to declare an object of type List<string, Customer> than taking the time to create my own collection or some other way of solving the problem. I certainly agree that they can create large amounts of complexity if used in abundance, but I feel like there are plenty of other languages features that have the same effect.

Leave a Reply