comp.lang.ada
 help / color / mirror / Atom feed
From: bobduff@world.std.com (Robert A Duff)
Subject: Re: [Q] To initialise or not.
Date: 1996/04/26
Date: 1996-04-26T00:00:00+00:00	[thread overview]
Message-ID: <DqHGKG.Jvp@world.std.com> (raw)
In-Reply-To: 318109B1.3B54AFBF@escmail.orl.mmc.com


In article <318109B1.3B54AFBF@escmail.orl.mmc.com>,
Theodore E. Dennison <dennison@escmail.orl.mmc.com> wrote:
>I suspect it is silent on this issue, because the Ada (83) LRM is 
>NOT silent on the issue. Section 3.2.1(18) reads:
>"The execution of a program is erroneous if it attempts to evaluate a
> scalar variable with an undefined value."

Yes, but how do you ensure that your program is not erroneous in this
way?  One way, which the earlier poster is asking about, is to have a
rule that all variables will be explicitly initialized on the
declaration.  The question is: does this coding convention help or hurt?
On the one hand, it prevents the erroneous cases.  On the other hand, it
causes variables to be initialized to bogus values, and what *would*
have been an erroneous read of an uninitialized variable, is, instead, a
read of a wrong value from that variable.

My personal coding convention is to *not* initialize variables if I know
they will be properly initialized later.  E.g.:

    declare
        X: Integer;
    begin
        if ... then
            X := 12;
        else
            X := 13;
        end if;
        ... -- some reads of X

In the above, I would *not* write "X: Integer := 0;".

The problem is, of course, that if you make a mistake, the program will
do who-knows-what.  But I still think it makes the code more readable to
avoid that bogus initial value of 0.

I would very much like to see compilers check these cases at run time.

I was going to suggest pragma Initialize_Scalars, but the original
poster said Ada 95 features are out of the question.

As an aside, I always write ":= null;" when I want to *rely* on the
initial value of a pointer, even though I know that pointers are always
default-initialized to null.

- Bob




  reply	other threads:[~1996-04-26  0:00 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-04-25  0:00 [Q] To initialise or not JP Thornley
1996-04-26  0:00 ` Ken Garlington
1996-04-26  0:00   ` Robert A Duff
1996-04-27  0:00   ` Robert Dewar
1996-04-27  0:00     ` Robert A Duff
1996-04-26  0:00 ` Robert I. Eachus
1996-04-26  0:00 ` Ken Garlington
1996-04-26  0:00 ` Theodore E. Dennison
1996-04-26  0:00   ` Robert A Duff [this message]
1996-04-27  0:00     ` Thorsten Behrens
1996-04-28  0:00       ` Robert Dewar
1996-04-30  0:00       ` mjp
1996-05-02  0:00       ` Bob Gilbert
1996-04-27  0:00 ` Robert Dewar
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox