comp.lang.ada
 help / color / mirror / Atom feed
From: "Thorsten Behrens" <thorsten.behrens@dorunth.hb.north.de>
Subject: Re: [Q] To initialise or not.
Date: 1996/04/27
Date: 1996-04-27T00:00:00+00:00	[thread overview]
Message-ID: <75602294@dorunth.hb.north.de> (raw)
In-Reply-To: DqHGKG.Jvp@world.std.com


Captain, bei Sternzeit Fri, 26 Apr 1996 18:55:28 GMT empfingen wir folgende
Nachricht von Robert A Duff:

>...
> 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;".

This initialization would indeed make no sense. But in the example above
(and in many similar cases) you could have used:

   declare
     X :  INTEGER
       := 12;
       -- 'X' is initialized to 12 here because ...
       -- It will be changed to '13' under the following circumstances ...
       -- in the code piece following.
   begin
     if not ... then
       X := 13;
     end if;
     ... -- some reads of 'X'

Of course, you would have to write some additional comment. But may be this
adds some details to your source some other people reading it may need to
get a deeper understanding (of course not in this simple example).

I tend to give all my declarations some initial value (if possible) _and_
an initial comment (giving a shorter or longer text about its usage and
the background of this variable). In my eyes this saves me from having
non-initialized variables (causing errors which are sometimes nasty to
find) and it saves people reviewing my sources from wasting too much time
to guess what I was thinking when "inventing" a variable (normally, I would
not use a variable called 'X', but instead would have called it
'Some_Meaningful_Name').

BTW, a former colleague of mine called me a 'secretary' when he found too
many comments in my source code. That the other side of the medal.

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

You could use some kind of software review tools for this.

-----------------------------------------------------------------------------
- Thorsten Behrens      ///    Voice : +49 421 351252                       -
- Buddestr. 39      \\\///     e-mail: thorsten.behrens@dorunth.hb.north.de -
- 28215 Bremen       \XX/                                                   -
-----------------------------------------------------------------------------
- aka Aggli 'if it moves, kill it' the Fist                                 -
-----------------------------------------------------------------------------





  reply	other threads:[~1996-04-27  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 ` Theodore E. Dennison
1996-04-26  0:00   ` Robert A Duff
1996-04-27  0:00     ` Thorsten Behrens [this message]
1996-04-28  0:00       ` Robert Dewar
1996-04-30  0:00       ` mjp
1996-05-02  0:00       ` Bob Gilbert
1996-04-26  0:00 ` Ken Garlington
1996-04-26  0:00 ` Robert I. Eachus
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-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