comp.lang.ada
 help / color / mirror / Atom feed
From: Robert Dewar <robert_dewar@my-deja.com>
Subject: Re: if statements
Date: Thu, 09 Nov 2000 05:24:28 GMT
Date: 2000-11-09T05:24:28+00:00	[thread overview]
Message-ID: <8udce8$1qi$1@nnrp1.deja.com> (raw)
In-Reply-To: 3A09A39F.2822C01B@cepsz.unizar.es

In article <3A09A39F.2822C01B@cepsz.unizar.es>,
  Alejandro Villanueva <190921@cepsz.unizar.es> wrote:
> Robert Dewar wrote:
>
> > In article <3A078B6F.D34B024B@erols.com>,
> >   Daniel Allex <dallex@erols.com> wrote:
> > > procedure Swap ( A,     B : in out Integer ) is
> > >       Temp : Integer := 0;
> > >    begin
> > >       if A <= B then
> > >          Temp := A;
> > >          A := B;
> > >          B := Temp;
> > >       end if;
> > >    end;
> >
> > Finally he gets someone to do his homework for him, though
> > not very competently (this looks like another student
> > tackling the excercise for the first time :-)
> >
>
> Robert, you do always the same... why do you have to say that?
> It IS a valid solution, and not the solution a geek will give
> after all. And MOST important: it WORKS!

If there is one thing in the Ada world that one expects it is
to get away from th idea that the MOST important thing about
code is that "it WORKS". Ada is about building reliable,
maintainable software, and the fact that it works is necessary
but no where *near* sufficient.

Let's look at the example above and see what's wrong with it

> > > procedure Swap ( A,     B : in out Integer ) is
> > >       Temp : Integer := 0;
> > >    begin
> > >       if A <= B then
> > >          Temp := A;
> > >          A := B;
> > >          B := Temp;
> > >       end if;
> > >    end;

First, the big chunk of white space between A, and B seems
quite gratuitous, I cannot think of any good excuse for it.

Second, the initialization of Temp is confusing and unnecessary.

Third, the indentation is non-standard. The RM suggests a
standard indentation, it should be used almost always.

Fourth, it is good practice to always put end labels for
procedures.

Fifth, Temp is a variable, when it should be a constant. People
very much overuse variables.

Sixth, Temp is defined too globally, you want to be easily
able to see that Temp is just a local variable for the Swap
and that its value is dead on exit.

Seventh, the name Swap is a very poor choice for the procedure,
since this is not what it does.

Eighth, why on earth swap the elements if they are equal, that
seems silly!

I would write the above as:

   procedure Conditional_Swap (A, B : in out Integer) is
   begin
      if A < B then
         declare
            Temp : constant Integer := A;
         begin
            A := B;
            B := Temp;
         end;
      end if;
   end Conditional_Swap;

I actually would prefer as a matter of style to introduce
a separate procedure called Swap, so that the guts of the
procedure would look like

    if A < B then
       Swap (A, B);
    end if;

Well all this is of course overkill for such a trivial
statement (one must wonder why anyone who finds it less
trouble to write a message to CLA than to solve such a
trivial problem themselves), but in fact the lesson to
be learned even at a very trivial level is precisely the
sort of attitudes to correct style that can then hopefully
scale up to larger examples.

So there you are, Robert's complete solution to the problem
first posed (after the deadline unfortunately :-)

I don't think that will make the plane crash :-) Though it
may be considered pedantic. As I say, bringing out the full
weight of careful style considerations for such a simple
program is overkill, but you have to start somewhere.


Sent via Deja.com http://www.deja.com/
Before you buy.



  parent reply	other threads:[~2000-11-09  5:24 UTC|newest]

Thread overview: 168+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-11-03 14:42 if statements sc297
2000-11-03  0:00 ` David Starner
2000-11-07  0:00   ` John English
2000-11-13  0:00   ` Dale Pontius
2000-11-03 16:09 ` Larry Kilgallen
2000-11-03  0:00   ` tmoran
2000-11-03  0:00     ` David Starner
2000-11-05  0:00       ` tmoran
2000-11-06  0:00         ` David Starner
2000-11-08  0:14           ` Alejandro R. Mosteo
2000-11-08  0:00             ` Alejandro Villanueva
2000-11-08  0:00               ` Georg Bauhaus
2000-11-09  3:50               ` Ken Garlington
2000-11-07  0:00         ` John English
2000-11-03  0:00     ` Larry Kilgallen
2000-11-06  0:00       ` Brian Orpin
2000-11-07  0:00     ` John English
2000-11-08  5:22       ` Ken Garlington
2000-11-06  0:00 ` Daniel Allex
2000-11-07  0:00   ` Robert Dewar
2000-11-07  0:00     ` Larry Kilgallen
2000-11-08  0:00     ` Ada student homework ? Nicolas Brunot
2000-11-08  0:00       ` Robert Dewar
2000-11-08  0:00         ` Nicolas Brunot
2000-11-08  0:00           ` Ted Dennison
2000-11-08  0:00             ` Lao Xiao Hai
2000-11-08  0:00             ` Nicolas Brunot
2000-11-08  0:00               ` Jerry Petrey
2000-11-09  0:00             ` Marin David Condic
2000-11-10  2:15               ` Ken Garlington
2000-11-10  0:00                 ` Marin David Condic
2000-11-18  0:00               ` John Magness
2000-11-19  0:00                 ` Robert Dewar
2000-11-19  0:00                   ` Larry Kilgallen
2000-11-20  0:00                 ` John English
     [not found]                 ` <3A2827A9.B54C260@ebox.tninet.se>
     [not found]                   ` <1dtW5.25958$6W1.1458704@news.flash.net>
2000-12-12 20:52                     ` Stefan Skoglund
     [not found]                       ` <dKo%5.144$PE5.16970@read2.inet.fi>
2000-12-19 16:36                         ` election chit-chat (was Re: Ada student homework ?) Robert Dewar
2000-11-08  0:00           ` Ada student homework ? Larry Kilgallen
2000-11-08  0:00             ` Nicolas Brunot
2000-11-14  0:00               ` Ronald Cole
2000-11-08  0:00       ` Larry Kilgallen
2000-11-08  0:00         ` Nicolas Brunot
2000-11-08  0:00           ` Brian Rogoff
2000-11-09  0:00             ` Nicolas Brunot
2000-11-09  0:00               ` Robert Dewar
2000-11-10  0:08                 ` Ehud Lamm
2000-11-10  0:00               ` Brian Rogoff
2000-11-08  0:00           ` Lao Xiao Hai
2000-11-09  0:00             ` Nicolas Brunot
2000-11-09  0:00               ` Lao Xiao Hai
2000-11-10  0:00                 ` Nicolas Brunot
2000-11-10  0:00                   ` Larry Kilgallen
2000-11-10  0:00                     ` Nicolas Brunot
2000-11-10  0:00                       ` Dan Nagle
2000-11-10  0:00                         ` Nicolas Brunot
2000-11-10  0:00                           ` Larry Kilgallen
2000-11-10  0:00                             ` Nicolas Brunot
2000-11-11  0:00                               ` Robert Dewar
2000-11-12  6:54                                 ` Brian Rogoff
2000-11-12  0:00                                   ` Robert Dewar
2000-11-11  0:00                           ` Robert Dewar
2000-11-13  0:00                             ` Nicolas Brunot
2000-11-13  0:00                               ` Robert Dewar
2000-11-13  0:00                                 ` gdemont
2000-11-13  0:00                                 ` Ken Garlington
2000-11-13  0:00                                 ` gdemont
2000-11-13  0:00                                   ` Ada student homework ?[OS/2 Comments] tjerick
2000-11-14  0:00                                   ` Ada student homework ? Georg Bauhaus
2000-11-14  5:16                                   ` Robert Dewar
2000-11-14  0:00                                     ` Nicolas Brunot
2000-11-15  0:00                                       ` Robert Dewar
2000-11-15  0:00                                         ` Ada student homework ? I give up Nicolas Brunot
2000-11-15  0:00                                           ` Robert Dewar
2000-11-14  0:00                                     ` Ada student homework ? John English
2000-11-14  0:00                                       ` gdemont
2000-11-14  0:00                                       ` Ole-Hjalmar Kristensen
2000-11-13  0:00                                 ` Nicolas Brunot
2000-11-13  0:00                                   ` Pascal Obry
2000-11-14  0:00                                 ` dmitry6243
2000-11-15  0:00                             ` Larry Kilgallen
     [not found]                             ` <3A0FCBAD.824095C6@cadwin.cOrganization: LJK Software <5E93Ivxu6GwO@eisner.decus.org>
2000-11-18  0:00                               ` Robert Dewar
2000-11-20  0:00                     ` Wes Groleau
2000-11-20  0:00                       ` Larry Kilgallen
2000-11-10  0:00                 ` Nicolas Brunot
2000-11-10  0:00                   ` Marin David Condic
2000-11-10  0:00                     ` Ole-Hjalmar Kristensen
2000-11-10  0:00                     ` Nicolas Brunot
2000-11-10  0:00                       ` David Starner
2000-11-11  0:00                       ` Robert Dewar
2000-11-13  0:00                         ` Nicolas Brunot
2000-11-10  0:00                     ` Randy Brukardt
2000-11-11  0:00                       ` Marin David Condic
2000-11-15  0:00                         ` Lao Xiao Hai
2000-11-18  0:00                           ` Robert Dewar
2000-11-18  0:00                             ` tmoran
2000-11-19  0:00                               ` Robert Dewar
2000-11-19  0:00                                 ` tmoran
2000-11-19  0:00                                   ` Robert Dewar
2000-11-20  0:16                                     ` tmoran
2000-11-20  0:00                                       ` Portability accross compilers Nicolas Brunot
2000-11-20  0:00                                         ` Robert Dewar
2000-11-20  3:18                                       ` Ada student homework ? Robert Dewar
2000-11-20  5:55                                         ` tmoran
2000-11-20  0:00                                           ` Robert Dewar
2000-11-20  0:00                                 ` Wes Groleau
2000-11-19  0:00                             ` Lao Xiao Hai
2000-11-11  0:00                   ` Robert Dewar
2000-11-13  0:00                     ` gdemont
2000-11-10  0:00                 ` mjsilva
2000-11-08  0:00           ` Chad R. Meiners
2000-11-08  0:00             ` Ted Dennison
2000-11-09  5:11               ` Robert Dewar
2000-11-18  0:00               ` John Magness
2000-11-09  5:04           ` Robert Dewar
2000-11-10  0:27             ` Subsetting Ada Ted Dennison
2000-11-08  0:00     ` if statements Alejandro Villanueva
2000-11-08  0:00       ` Daniel Allex
2000-11-09  3:54       ` Ken Garlington
2000-11-09  5:24       ` Robert Dewar [this message]
2000-11-09  0:00         ` Ken Garlington
2000-11-09  0:00           ` Robert Dewar
2000-11-10  0:12             ` Ehud Lamm
2000-11-10  2:09             ` Ken Garlington
2000-11-20  0:00               ` Wes Groleau
2000-11-20  0:00             ` Wes Groleau
2000-11-21  0:00               ` Robert Dewar
2000-11-22  0:00                 ` Wes Groleau
2000-11-09  0:00         ` Marin David Condic
2000-11-09  0:00           ` Robert Dewar
2000-11-10  0:00             ` Larry Kilgallen
2000-11-10  1:58           ` Ken Garlington
2000-11-10  3:53             ` Robert Dewar
2000-11-09  0:00         ` constants v variables (was Re: if statements) Martin Dowie
2000-11-09  0:00           ` Robert A Duff
2000-11-09  0:00             ` Florian Weimer
2000-11-10  2:13               ` Ken Garlington
2000-11-10  5:59                 ` Brian Rogoff
2000-11-09  0:00             ` Martin Dowie
2000-11-09  0:00             ` Robert Dewar
2000-11-09  0:00           ` Robert Dewar
2000-11-07  0:00   ` if statements dmitry6243
2000-11-07  0:00     ` Jean-Pierre Rosen
2000-11-08  0:00       ` dmitry6243
2000-11-07  0:00   ` Wes Groleau
2000-11-08  0:00     ` John English
2000-11-08  0:00       ` Wes Groleau
2000-11-09  3:43         ` Ken Garlington
2000-11-07  0:00   ` John English
2000-11-07  0:00   ` Ken Garlington
2000-11-07  0:00     ` John English
2000-11-07  0:00       ` Robert Dewar
2000-11-08  0:00         ` John English
2000-11-08  0:00           ` Georg Bauhaus
2000-11-09  4:58           ` Robert Dewar
2000-11-09  0:00             ` Ken Garlington
2000-11-09  0:00               ` Designing test suites (was: if statements) Larry Kilgallen
2000-11-09  0:00             ` if statements John English
2000-11-08  5:29       ` Ken Garlington
2000-11-08  0:00         ` John English
2000-11-08  0:00           ` Ken Garlington
2000-11-07  0:00 ` John English
2000-11-07  0:00   ` Robert Dewar
2000-11-10  6:09 ` DJack143
2000-11-11  0:00   ` Here we go again (Was: if statements) Frank Christiny
  -- strict thread matches above, loose matches on Subject: below --
2000-11-09  0:00 if statements James Hassett
2000-11-10  0:02 ` Robert Dewar
2000-11-14  0:00 Duncan Sands
2000-11-14  0:00 ` Ken Garlington
replies disabled

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