comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: if-then-no-else  Programming
Date: Tue, 19 Apr 2016 14:51:05 -0500
Date: 2016-04-19T14:51:05-05:00	[thread overview]
Message-ID: <nf6279$5r1$1@loke.gir.dk> (raw)
In-Reply-To: 1mlx1gf.ebrae11jak5tyN%csampson@inetworld.net

"Charles H. Sampson" <csampson@inetworld.net> wrote in message 
news:1mlx1gf.ebrae11jak5tyN%csampson@inetworld.net...
> It's hard to believe that it's been over six years since I wrote a line
> of code for profit. If what my son tells me, there's been what I
> consider a major change in software engineering during that time.
>
> He says that there's a move to ban the use of the else-statement. The
> preferred approach is to execute the else-part first, then change the
> effect if the if-condition is satisfied. For example:
>
>   Variable := 3;
>   if <condtion> then
>      Variable := 1;
>   end if;
>
> In addition to some other goodness attributes, this is supposed to be
> clearer than the if-then-else form.
>
> Is he right? (He's not really a coder. His experience is in wire-frame
> animation but he's being forced into coding by the job market.) If he's
> not right, have any of you even heard of an area of the software
> "profession" where this position is held?

This sounds like nonsense to me. It wouldn't make sense for the majority of 
the code that we have:

    if condition then
        Do_It;
    else -- Can't get here.
        Internal_Error ("Impossible branch");
    end if;

and

    if Is_Float (Expr.Typ) then
        Generate_Float_Code (Expr.Typ);
   else
        Generate_Integer_Code (Expr.Typ);
   end if;

I think if we generated both kinds of code for a float expression, we 
wouldn't have been in the compiler business for long. ;-)

Indeed, RRS has a style rule which is the exact opposite of his suggestion. 
We require either an else or a comment that no else is needed for most if 
statements. We found that we had cases like:

    if condition then
         Do_Something;
    end if;

and there would be a bug because nothing was done if condition was False. 
Errors of omission are the hardest things to find, and we hoped to reduce 
the number of them by at least requiring the programmer to think about all 
of the possibilities and documenting that they did so. Thus, the above would 
have to be written:

    if condition then
         Do_Something;
    -- else nothing needed.
    end if;

so it's obvious that the reverse condition was considered.

There are of course cases where the suggested form would work, but even in 
those one needs to document that the else was omitted on purpose and not 
just forgotten. Otherwise, a future maintainer has to guess which case is 
involved and that can cause one to waste a lot of time on things that are 
intended.

                               Randy.



  parent reply	other threads:[~2016-04-19 19:51 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-19  2:24 if-then-no-else Programming Charles H. Sampson
2016-04-19  3:08 ` Dennis Lee Bieber
2016-04-19  3:19 ` Jeffrey R. Carter
2016-04-19  6:18 ` Nasser M. Abbasi
2016-04-19  7:55 ` Dmitry A. Kazakov
2016-04-19 12:17 ` G.B.
2016-04-20 22:26   ` Martin
2016-04-19 13:27 ` gautier_niouzes
2016-04-19 19:51 ` Randy Brukardt [this message]
2016-04-19 22:40   ` Shark8
2016-04-20 22:35     ` Randy Brukardt
2016-04-20  7:56   ` Charles H. Sampson
2016-04-20 11:26     ` Dennis Lee Bieber
2016-04-20 12:32       ` G.B.
2016-04-20 12:36         ` G.B.
2016-04-20 23:07     ` Jeffrey R. Carter
2016-04-19 20:32 ` Charles H. Sampson
replies disabled

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