Re: Newbie (Post#1): Help with Array of Objects

From: Chris Smith (cdsmith_at_twu.net)
Date: 07/12/04


Date: Sun, 11 Jul 2004 20:34:59 -0600

Java Architect wrote:
> This reply is exactly right, but there's more to it. In java, unlike member
> variables, local variables are not implicitly initialized, and you cannot
> use uninitialized variables except to assign a value to them.

Right.

> The point is that, when declaring a local variable, you should always give
> it an initial value.

Yikes! That's about exactly the wrong conclusion. This only makes
sense if you picture yourself in a war against the compiler to make your
code compile, which is a dubious and even destructive world-view. If
your goal is real working code, you'll cooperate with the compiler to
help find bugs and make your code work properly. Ironically, it's
exactly the opposite case in languages that don't have definite
assignment checks (for example, C and C++); there you DO want to make
sure you give every local variable an initial value.

Look at it this way. There are several possible results to your making
a mistake and accidentally using a variable that doesn't have a useful
value. They are as follows:

    1. The code always fails at build. Best possible scenario.
    2. The code always fails at runtime. Good scenario.
    3. The code can fail deterministically. Bad scenario.
    4. The code can fail non-deterministically. Worst case scenario.

In C and C++, the default is #4, but you can get to #3 by giving every
local variable a value when it's declared, as a matter of policy.
That's a good step, because it makes it easier to isolate and fix the
problem once it's discovered. Sure you still won't find the problem
until testing or quite possibly production, but there's no easy way to
do any better, so you take what you can get.

In Java, the default is #1, but you can get to #3 by giving every local
variable a value when it's declared, as a matter of policy. That's a
pretty bad direction to be going, because you WERE going to be told
about the bug automatically as a matter of course, but you've just lied
to the compiler to make it shut up, and now you won't find the bug until
testing, or quite possibly production. You could lose customers over
this kind of thing. The compiler is not your enemy; repeat as
necessary.

In practice, definite assignment checking helps you to find all kinds of
issues, from actual incorrect treatment of that variable all the way to
mistaken control flow, such as forgetting to abruptly terminate a method
when responding to an exception. In any case, if you get that error and
don't expect it, it's time to start looking for what's really wrong.

There are, actually, a few situations in which definite assignment
checking fails even though a variable is logically guaranteed to be
assigned. For the most part, you can learn to avoid these by using the
'else' and 'default' keywords liberally. In a few situations, though,
you really do need to assign a false initial value to avoid a compiler
error. That indicates a pretty tricky piece of code, you you can
simultaneously add the false initialization *and* a comment informing
the reader about what's going on. That has never happened to me in
seven years of writing professional Java code; but I can easily
reproduce it in example code -- the point is that this should be
exceedingly rare.

So in the end, it's a really bad idea to always assign an initial value
to a variable at its declaration. That's a nice advantage of Java; the
right thing to do, at least in this case, is exactly what makes sense.
No artificial rules required.

-- 
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation


Relevant Pages

  • Re: Newbie (Post#1): Help with Array of Objects
    ... I code Java in a TDD environment, ... > sense if you picture yourself in a war against the compiler to make your ... Best possible scenario. ... The code always fails at runtime. ...
    (comp.lang.java.help)
  • Using final with local variables
    ... A question for the Java gurus. ... That makes sense because the compiler is directly writing machine code ... and can make use of the hint if it wants to. ... I am talking about local variables, ...
    (comp.lang.java.programmer)
  • Re: newbie help interpreting this code from book
    ... > others might have a garbage value without initialization. ... local variables may have arbitrary values and should not be used. ... the picture in Java is much different. ... the compiler has to be able to prove to ...
    (comp.lang.java.help)
  • Re: Cpp Considered Harmful
    ... >> programming language, the compiler, the IDE, the libraries, etc. ... source file, the implementation shall locate the declaration, (and ... to name one of the defining source files on the command line that initiates ... The counterpart to this in Java is accomplished using the following: ...
    (comp.lang.cpp)
  • Re: wie Array für statische Methoden
    ... > auf gar keinen Fall Java empfehlen;) ... Ich hab mal mit virtuellen Methoden auf einem Microkontroller experimentiert und bin zu dem Schluss gekommen: ... > Es ist ja gerade der Vorteil bei Java einen Compiler ... > Dinge zu optimieren, ...
    (de.comp.lang.java)