Re: Newbie (Post#1): Help with Array of Objects
From: Chris Smith (cdsmith_at_twu.net)
Date: 07/12/04
- Next message: Java Architect: "Re: Newbie (Post#1): Help with Array of Objects"
- Previous message: Chris Smith: "Re: Newbie (Post#1): Help with Array of Objects"
- In reply to: Java Architect: "Re: Newbie (Post#1): Help with Array of Objects"
- Next in thread: Java Architect: "Re: Newbie (Post#1): Help with Array of Objects"
- Reply: Java Architect: "Re: Newbie (Post#1): Help with Array of Objects"
- Reply: Roedy Green: "Re: Newbie (Post#1): Help with Array of Objects"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Java Architect: "Re: Newbie (Post#1): Help with Array of Objects"
- Previous message: Chris Smith: "Re: Newbie (Post#1): Help with Array of Objects"
- In reply to: Java Architect: "Re: Newbie (Post#1): Help with Array of Objects"
- Next in thread: Java Architect: "Re: Newbie (Post#1): Help with Array of Objects"
- Reply: Java Architect: "Re: Newbie (Post#1): Help with Array of Objects"
- Reply: Roedy Green: "Re: Newbie (Post#1): Help with Array of Objects"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|