Re: Java Generics and Erasure foobar



HackerBob wrote:
Maybe it's me but I have been reviewing this so called major feature of 1.5 and I cannot see the value.

I think the mistakes it detects at compile time are trivial, in that a novice would make those mistakes. So what, if I find type issues at runtime that's what unit testing is for. Now it takes longer to develop <uglier> code for little to no additional value, afterall generics get erased.

I mean if I mix a String with an Integer this will come out in unit testing and the result will be the same. I still have to unit test, I still have to fix the code.

I think Sun should leave the core language alone and let the developer worry about run time exceptions from poor design and coding.

You've not worked on large projects, I take it. The value of being able to specify the contents of a collection is enormous. Take one (non-Java) project I work on: it returns stuff in the form of an enumerator, which just has a method equivalent to |Object getNext()|. The caller is responsible for casting that to the necessary type. But wait, what class do I cast to? Is it a card? A string? An integer? An RDF resource?

So one of the primary features of generics is this: it's self-documenting. You know what's in a List<String>, but List tells you nothing about its contents. Although I personally strive to be precise in explaining what is returned in the documentation, many people are not that precise. After all, "returns an enumerator of all the values" describes what it does very well, doesn't it?

Compile time safety is another vital feature. You may say "it'll come out in unit tests," but are you really that confident that every single line of your program is covered by enough unit tests to validate every single possible parameter and side effect that might happen? I doubt that your project has every single line tested let alone with all the necessary setups to ensure that it works in all cases.

If you think that generics provide no value, then try writing in a language whose notion of compile-time checking is limited to "is this syntactically valid." Trust me, the first minor refactoring you do in such a language will have you pining for stringent compile-time checking. For me, this comes from actual experience, where I found a change I had missed, despite a month of stringent testing, completely by accident.

A program is only as good as its worst piece of code. It takes but a single statement to crash a program. The harder you make it to write such a statement, the better off your program will be. Generics makes it harder to write incorrect code; that alone should be sufficient reason to use it.

--
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
.



Relevant Pages

  • Re: Java Generics and Erasure foobar
    ... I mean if I mix a String with an Integer this will come out in unit ... So one of the primary features of generics is this: ... Compile time safety is another vital feature. ... out in unit tests," but are you really that confident that every ...
    (comp.lang.java.programmer)
  • Re: A Generic Generics Problem
    ... You'll get a warning at compile time, ... I suppose I'm asking for the impossible: the type safety of generics ... underlying object preserve the type information after it's created, ... List<String> always knows that it can only take a String because the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Delphi: generics coming
    ... checkable at compile time. ... Generics are compile time checkable and allow you to perform any operation ... | so far these reasons aren't compelling. ... framework types, not just lists, and that has saved us thousands of lines of ...
    (borland.public.delphi.non-technical)
  • Re: Cant get value from DataSet without explicity type casting?
    ... > Each column has a datatype. ... If it's a string, it will return a ... > the compiler doesn't know what it's going to be at compile time. ... >> And I also can't get the DateTime into a variable. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: "Professional Javascript" review
    ... | A string value is a member of the type String ... A variable is not a member of any Type. ... Types limit the values that a variable can hold ... | typing helps detect errors at compile time. ...
    (comp.lang.javascript)