comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthew_heaney@acm.org>
Subject: Re: Software landmines (was: Why C++ is successful)
Date: 1998/08/29
Date: 1998-08-29T00:00:00+00:00	[thread overview]
Message-ID: <m3g1egsug4.fsf@mheaney.ni.net> (raw)
In-Reply-To: 6s6v4i$mht@dfw-ixnews8.ix.netcom.com

Richard D Riehle <laoXhai@ix.netcom.com> writes:

>  So, in this respect, C++ initialization lists better express the
>  "binding" requirement than Ada assignments.  

One of the areas in Ada in which the assignment vs initialization issue
arises is in the declaration of limited types:

declare
   O : LT;
begin
   Initialize (O);
   <use O>

The thing I find distasteful here is that the initialization of object O
occurs after its declaration.  Sometimes this means the state of O is
undefined.  (However, many times limited objects come with a defined,
default state.  For example, Is_Open is defined to return False for a
newly declared instance of File_Type.)

I've advocated in the past that the language syntax should be amended to
add an initialization operation ("constructor"):

declare
   File : File_Type'Open (In_Mode, "matt.dat");
begin

We could even use the constant keyword in such a declaration.

> Also, several consecutive assignment statements in Ada will often be
> more readable than the parentheticaly clauses of function returns that
> serve as their substitute.

Agree.  I often see programmers use many nested function calls in an
expression.  What I have to do then is to mentally unravel the
expression.  So why not save me the trouble?  Use a declare block to
declare a few intermediate objects, and feed them to the ultimate
subprogram call.

> The suggestion that we use fewer assignment statements is liable to 
> create the same mess that "gotoless" programming fomented in the COBOL
> world.  

Let's make sure everyone knows what this means:

o It DOES mean "don't use a variable when a constant will do."

o It does NOT mean "use nested function calls to return intermediate
values, instead of declaring intermediate constant objects"

Yes, you really should use constant object declarations to hold
intermediate values, even though this requires a few more "assignments."
(The scare quotes are there to remind the reader that the declaration of
a constant object isn't really an assignment.)

> Ada is designed to encourage the use of the assigment statement.

Yes.  Far too few programmers use declare blocks and constant object
declarations to simplify complex expressions.  Instead of

Op (Get_A (10), Get_B (X), Get_C (Y));

it's better to do

declare
   A : constant Integer := Get_A (10);
   B : constant Float := Get_B (X);
   C : constant Boolean := Get_C (Y);
begin
   Op (A, B, C);
end;

As we have noted, the declarations above aren't really "assignment"
statements.  They are "binding" operations.  The distinction would be
more clear (and consistent) if the language allowed you to say:

declare
   A : Integer is Get_A (10);
   B : Float is Get_B (X);
   C : Boolean is Get_C (Y);
begin
   Op (A, B, C);
end;

The "renames" clause is a binding operator in Ada (although technically
it binds a name to an object, not a value).  A hip way to do the above
directly in the language is

declare
   A : Integer renames Get_A (10);
   B : Float renames Get_B (X);
   C : Boolean renames Get_C (Y);
begin
   Op (A, B, C);
end;

That way we can avoid the use of the assignment operator, so the
declaration won't be confused with an assignment statement.  

It's a bit of a bummer that we have 3 ways ("constant T :=", "renames",
"is") of doing essentially the same thing.  Why not just one way?

> Suggesting that we do not use it so often is probably
> counterproductive and can only be realized with some important changes
> in the language.

An unambiguous way to say this is that, in a declaration, you should use
the keyword "constant" whenever the object doesn't change its value.

Yet another way to say this is "don't use assignment when you really
mean binding."

Matt

P.S. About the meaning of binding: let me dig up my books on programming
language theory.  You usually see it mentioned in books about functional
programming, and in explications of denotational semantics.







  reply	other threads:[~1998-08-29  0:00 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-08-22  0:00 Software landmines (was: Why C++ is successful) dewar
1998-08-23  0:00 ` Dale Stanbrough
1998-08-23  0:00   ` dewar
1998-08-23  0:00   ` Andi Kleen
1998-08-24  0:00 ` Richard D Riehle
1998-08-25  0:00   ` Andi Kleen
1998-08-25  0:00     ` Brian Rogoff
1998-08-25  0:00   ` dennison
1998-08-28  0:00   ` Matthew Heaney
1998-08-28  0:00     ` Richard D Riehle
1998-08-29  0:00       ` Matthew Heaney [this message]
1998-09-06  0:00         ` John G. Volan
1998-09-07  0:00           ` Mats Weber
1998-09-07  0:00             ` dewarr
1998-09-08  0:00               ` Tucker Taft
1998-09-08  0:00                 ` Precalculation of parameters (was: Software landmines) dennison
1998-09-09  0:00                   ` Tucker Taft
1998-09-09  0:00                     ` dennison
1998-09-10  0:00                       ` Tucker Taft
1998-09-10  0:00                         ` dewarr
1998-09-16  0:00               ` Software landmines (was: Why C++ is successful) Matthew Heaney
1998-08-29  0:00       ` Matthew Heaney
     [not found]   ` <35eca5d9.4354839@news.geccs.gecm.com>
1998-09-01  0:00     ` Richard D Riehle
  -- strict thread matches above, loose matches on Subject: below --
1998-08-06  0:00 Why C++ is successful Robert Dewar
1998-08-07  0:00 ` harald.mueller
1998-08-07  0:00   ` Brian Rogoff
1998-08-07  0:00     ` Timothy Welch
1998-08-08  0:00       ` Robert Dewar
1998-08-08  0:00         ` Jeffrey C. Dege
1998-08-10  0:00           ` Laurent GUERBY
1998-08-12  0:00             ` Andy Ward
1998-08-14  0:00               ` Robert Dewar
1998-08-14  0:00                 ` Software landmines (was: Why C++ is successful) dennison
1998-08-15  0:00                   ` Thaddeus L. Olczyk
1998-08-16  0:00                   ` Robert Dewar
1998-08-17  0:00                     ` dennison
1998-08-18  0:00                       ` adam
1998-08-19  0:00                         ` Tucker Taft
1998-08-19  0:00                           ` adam
1998-08-19  0:00                       ` ell
1998-08-19  0:00                         ` Charles Hixson
1998-08-19  0:00                         ` adam
1998-08-19  0:00                           ` Dan Higdon
1998-08-20  0:00                             ` adam
1998-08-20  0:00                               ` Dan Higdon
     [not found]                               ` <m33eagru5g.fsf@mheaney.ni.net>
1998-08-31  0:00                                 ` Frank Adrian
1998-08-31  0:00                                   ` Robert I. Eachus
1998-08-31  0:00                                     ` Biju Thomas
1998-08-31  0:00                                       ` Robert Martin
1998-09-01  0:00                                         ` Martin Dowie
1998-09-01  0:00                                       ` Robert I. Eachus
1998-09-02  0:00                                         ` dennison
1998-09-01  0:00                                   ` dewarr
1998-09-06  0:00                                 ` Jonathan Guthrie
1998-08-20  0:00                           ` Ell
1998-08-21  0:00                             ` Ell
1998-08-21  0:00                               ` John Goodsen
1998-08-21  0:00                                 ` Ell
1998-08-21  0:00                                   ` Ell
1998-08-21  0:00                               ` Larry Brasfield
1998-08-21  0:00                                 ` Ell
1998-08-21  0:00                                 ` Jeffrey C. Dege
1998-08-20  0:00                                   ` Phlip
1998-08-21  0:00                                   ` Larry Brasfield
     [not found]                                   ` <DOSXjHE9T6DM9Jw9nAyaPxfz@news.rdc1.bc.wave.home.com>
1998-08-22  0:00                                     ` dewar
1998-08-24  0:00                                       ` dennison
1998-08-28  0:00                                         ` Matthew Heaney
1998-08-28  0:00                                           ` dennison
1998-08-30  0:00                                             ` Matthew Heaney
1998-09-06  0:00                                               ` John G. Volan
1998-08-31  0:00                                             ` Robert I. Eachus
1998-08-24  0:00                                       ` Martin Dowie
1998-08-24  0:00                                         ` Martin Dowie
1998-08-24  0:00                                           ` Mark A Biggar
1998-08-25  0:00                                             ` Martin Dowie
1998-08-25  0:00                                               ` Mark A Biggar
1998-08-26  0:00                                                 ` Martin Dowie
1998-08-25  0:00                                         ` adam
1998-09-22  0:00                                       ` Charles H. Sampson
1998-08-21  0:00                                 ` Bob Collins
1998-08-20  0:00                         ` Gerry Quinn
1998-08-16  0:00                   ` Jay Martin
1998-08-17  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