Re: Java Generics and Erasure foobar
- From: HackerBob <NoReply>
- Date: Mon, 29 Sep 2008 13:06:41 -0500
Joshua Cranmer <Pidgeot18@xxxxxxxxxxxxxxx> wrote in
news:gbp3g1$e8r$1@xxxxxxxxxxxxxxxxxxxx:
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.
The issue here is that you still have to code for valid scenarios. I may
not be able to test every possible scenario but I can specify/limit the
valid ones.
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.
So generics will make your code bug proof, more bug resistant? I do not
think so. I think you find your errors sooner in the process which does
add value but not enormous.
A program is only as good as its worst piece of code. It takes but aI do not think making it harder to write incorrect code is the correct
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.
goal.The goal should be to write correct code easier, more parameters
does not seem like the answer.
Overall, I understand what you are saying and I do see your point but
after years of C programming in a structured environment you learn to
declare what variables you are going to use and use them as they are
declared. Generics do not seem to be an enormous step forward, they feel
like a burden. I feel like I am forced to delcare methods and their
usage over and over. If I want a collection to be all strings I should
check that the members are an instance of type String before I add them
to the collection. This is the future so I will have to suck it up but I
do not have to like it. I think its the nature of the Java community
with shared files coming from different sources, some coded better than
others, that you need to have this type verification. I think Java is an
elegant language and as such more stringent anything is really less.
I think the short answer is that their are pros and cons to generics and
each developer will seem them differently.
.
- Follow-Ups:
- Re: Java Generics and Erasure foobar
- From: Lew
- Re: Java Generics and Erasure foobar
- From: Eric Sosman
- Re: Java Generics and Erasure foobar
- From: Joshua Cranmer
- Re: Java Generics and Erasure foobar
- References:
- Java Generics and Erasure foobar
- From: HackerBob
- Re: Java Generics and Erasure foobar
- From: Joshua Cranmer
- Java Generics and Erasure foobar
- Prev by Date: Re: Using Thread.sleep(..) and Swing Components
- Next by Date: Struts for swing
- Previous by thread: Re: Java Generics and Erasure foobar
- Next by thread: Re: Java Generics and Erasure foobar
- Index(es):
Relevant Pages
|
Loading