home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / ada / 3616 < prev    next >
Encoding:
Internet Message Format  |  1992-12-11  |  2.1 KB

  1. Path: sparky!uunet!think.com!ames!haven.umd.edu!mimsy!alex
  2. From: alex@cs.umd.edu (Alex Blakemore)
  3. Newsgroups: comp.lang.ada
  4. Subject: Re: Suggestion for multivalue boolean case for 9X
  5. Message-ID: <62774@mimsy.umd.edu>
  6. Date: 11 Dec 92 20:08:14 GMT
  7. References: <dnsurber.724028252@node_26400> <1992Dec11.011515.2859@saifr00.cfsat.honeywell.com>
  8. Sender: news@mimsy.umd.edu
  9. Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742
  10. Lines: 44
  11.  
  12. dunbar@saifr00.cfsat.honeywell.com (Mark Dunbar) writes:
  13. > Using Ada83, instead of the nested if statement, one could do the following:
  14. >   if    ( not Cond_1 ) and ( not Cond_2 ) and ( not Cond_3 ) then
  15. >   elsif ( not Cond_1 ) and ( not Cond_2 ) and (     Cond_3 ) then
  16. >   elsif ( not Cond_1 ) and (     Cond_2 ) and ( not Cond_3 ) then
  17. >   etc.
  18.  
  19. I've rarely encountered complex nested if statements where
  20. every combination of conditions had a completely different action.
  21. More often, several combinations share the same action, or parts of
  22. the same action.  In such cases, you can frequently improve things
  23. by combining and reordering tests - often removing a level of nesting.
  24.  
  25. for a simple example,
  26.  
  27.   if x then                               if not x then
  28.     if y then        can be rewritten       C;
  29.       A;                                  elsif y then
  30.     else                                    A;
  31.        B;                                 else
  32.     end if;                                 B;
  33.   else                                    end if;
  34.     C;
  35.   end if;
  36.  
  37. (assuming the conditions are free of side effects)
  38. A single level chain of ordered condition/action pairs is
  39. much easier to reason about than any nested logic.
  40.  
  41. I know it seems to picayune but I've found many errors
  42. by simplifying logical statements in mechanical ways
  43. like this - and almost always obtain a better understanding
  44. of the code as a side effect.
  45.  
  46. This should be ingrained after CS 101 and not be worth mentioning
  47. in such an esteemed forum as comp.lang.ada but many
  48. programmers dont seem to take the time to find the the
  49. simplest construct.
  50.  
  51.  
  52.  
  53. -- 
  54. ---------------------------------------------------
  55. Alex Blakemore alex@cs.umd.edu   NeXT mail accepted
  56.