Re: Newbie (Post#1): Help with Array of Objects
From: Java Architect (danc_at_dynamicresolve.com)
Date: 07/12/04
- Next message: Roedy Green: "Re: Newbie (Post#1): Help with Array of Objects"
- Previous message: Java Architect: "Re: Newbie (Post#1): Help with Array of Objects"
- In reply to: Chris Smith: "Re: Newbie (Post#1): Help with Array of Objects"
- Next in thread: Thomas Schodt: "Re: Newbie (Post#1): Help with Array of Objects"
- Reply: Thomas Schodt: "Re: Newbie (Post#1): Help with Array of Objects"
- Reply: Ryan Stewart: "Re: Newbie (Post#1): Help with Array of Objects"
- Reply: Chris Smith: "Re: Newbie (Post#1): Help with Array of Objects"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 11 Jul 2004 20:26:01 -0700
Difference of opinion and coding style. I have followed this rule since long
before I got into Java, and I get a result you failed to consider:
5. The code is well written and runs successfully.
I code Java in a TDD environment (have for some time), and my tests will
catch cases 1-4 for me. Not giving an initial assignment to a variable leads
to problems like (this is contrived, but I've seen many similar cases):
int a;
try {
a = 5;
Thread.sleep(1000);
}
catch (InterruptedException err)
{
}
int y = a;
Oooops... You get an initialization error here.
You could argue that there's no reason to put the assignment in the
try/catch. In this simplified case, you'd be correct. However, there are
scenarios where you don't know the initialization value until somewhere in
the try/catch but before an unrelated (and inconsequential) exception might
be thrown.
"Chris Smith" <cdsmith@twu.net> wrote in message
news:MPG.1b5ba6bb14d620509896f9@news.altopia.net...
> 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: Roedy Green: "Re: Newbie (Post#1): Help with Array of Objects"
- Previous message: Java Architect: "Re: Newbie (Post#1): Help with Array of Objects"
- In reply to: Chris Smith: "Re: Newbie (Post#1): Help with Array of Objects"
- Next in thread: Thomas Schodt: "Re: Newbie (Post#1): Help with Array of Objects"
- Reply: Thomas Schodt: "Re: Newbie (Post#1): Help with Array of Objects"
- Reply: Ryan Stewart: "Re: Newbie (Post#1): Help with Array of Objects"
- Reply: Chris Smith: "Re: Newbie (Post#1): Help with Array of Objects"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|