Generics in Java Are Evil?
Dean Wette's talk about Collections and Generics in J2SE 1.5 at the St. Louis Java Users Group tonight is livelier than usual.
Here's the gist of the talk:
- Generics are evil: The deeper I dig into the specification, the scarier it gets.
- Generics solves a problem that's not there: The kinds of errors that a type safe collection prevents are the trivial programmer errors that's easily detected with unit tests. IDE's can help with the drudgery of casting when working with Collections.
- JDK 1.5 generics uses erasure: None of the parameter type information is present in bytecode. Therefore instanceof doesn't work with generic types. Many intuitive programming constructs are illegal. The erasure decision was made so that Sun doesn't have to modify the JVM. They ended up modifying the JVM anyway.
- The new syntax is too complicated: I've been doing Java since 1996 and am pretty good at it. I'm having trouble with the syntax. Just imagine what a new Java programmer will feel.
- I would rather use C++ templates!
Dean did talk about several things that he likes: autoboxing, and the enhanced for loop. The java.util.concurrent classes were not covered tonight but were favorably mentioned.
Jeff Grigg urged programmers to take a look at C# for generics done right.
How would you like code like this?
public <T extends Comparable & Serializable> T foo(Collection<T> c1,
Collection<? extends T> c2) {
// ...
}
Re: Generics in Java Are Evil?
Erasure is the key flaw of Java generics. Without reflection or any performance boost, generics are nothing more than syntax sugar. We now have more ways of expressing the same concept, which means more complexity. People have to master both the old way and the new way, making Java increasingly difficult to master. Programs will inevitably contain a mixture of styles (some people use Vector, others use List, others use lots of <T> stuff).
I believe Java 1.5 is the pivitol moment when Java "jumps the shark". We have lost sight of what made Java great in the first place - it was easier than C++.
Re: Generics in Java Are Evil?
Eric's point is right on the money. Java Generics gives us syntactic candy that provides little or no real benefit, but plenty of opportunity for confusion and code that fails to work as intended when implemented by developers without enough experience to understand the consequences of type parameter bounding, wildcards, and erasure.
Indeed, not only is Java losing its simplicity in 1.5, but its elegance as well.
Re: Generics in Java Are Evil?
JAVA ha a lot of interesting design feature :
1. primitive type and wrapper object without operations, for example float have + to sum, but object Float dont have sum!!!, so waste programmer and machine time converting primitive type to object wrapper and then back.
2. strong typing make this language difficult to develop, but dont avoid problem at runtime for dynamic binding.
3. generics, help a little to avoid casting errors, but with type erasure make code almost impossible to debug at runtime, also make reflection useless.
How do you name a language wich is Difficult to develop,debug,mantein ?