comp.lang.ada
 help / color / mirror / Atom feed
From: alex@MIMSY.CS.UMD.EDU  (Alex Blakemore)
Subject: Re: Suggestion for multivalue boolean case for 9X
Date: 11 Dec 92 20:08:14 GMT	[thread overview]
Message-ID: <62774@mimsy.umd.edu> (raw)

dunbar@saifr00.cfsat.honeywell.com (Mark Dunbar) writes:
> Using Ada83, instead of the nested if statement, one could do the following:
>   if    ( not Cond_1 ) and ( not Cond_2 ) and ( not Cond_3 ) then
>   elsif ( not Cond_1 ) and ( not Cond_2 ) and (     Cond_3 ) then
>   elsif ( not Cond_1 ) and (     Cond_2 ) and ( not Cond_3 ) then
>   etc.

I've rarely encountered complex nested if statements where
every combination of conditions had a completely different action.
More often, several combinations share the same action, or parts of
the same action.  In such cases, you can frequently improve things
by combining and reordering tests - often removing a level of nesting.

for a simple example,

  if x then                               if not x then
    if y then        can be rewritten       C;
      A;                                  elsif y then
    else                                    A;
       B;                                 else
    end if;                                 B;
  else                                    end if;
    C;
  end if;

(assuming the conditions are free of side effects)
A single level chain of ordered condition/action pairs is
much easier to reason about than any nested logic.

I know it seems to picayune but I've found many errors
by simplifying logical statements in mechanical ways
like this - and almost always obtain a better understanding
of the code as a side effect.

This should be ingrained after CS 101 and not be worth mentioning
in such an esteemed forum as comp.lang.ada but many
programmers dont seem to take the time to find the the
simplest construct.



-- 
---------------------------------------------------
Alex Blakemore alex@cs.umd.edu   NeXT mail accepted

             reply	other threads:[~1992-12-11 20:08 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1992-12-11 20:08 Alex Blakemore [this message]
  -- strict thread matches above, loose matches on Subject: below --
1992-12-14 21:07 Suggestion for multivalue boolean case for 9X John Bollenbacher
1992-12-13 15:24 agate!spool.mu.edu!yale.edu!jvnc.net!gmd.de!Germany.EU.net!mcsun!sunic!se
1992-12-11 19:38 James Crigler
1992-12-11  1:15 Mark Dunbar
1992-12-10 22:57 Douglas N. Surber
replies disabled

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