home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / ada / 3660 < prev    next >
Encoding:
Text File  |  1992-12-14  |  4.4 KB  |  100 lines

  1. Newsgroups: comp.lang.ada
  2. Path: sparky!uunet!stanford.edu!bcm!aio!dnsurber
  3. From: dnsurber@lescsse.jsc.nasa.gov (Douglas N. Surber)
  4. Subject: Re: Suggestion for multivalue boolean case for 9X
  5. Message-ID: <dnsurber.724344545@node_26400>
  6. Sender: news@aio.jsc.nasa.gov (USENET News System)
  7. Organization: Lockheed Engineering and Sciences
  8. References: <dnsurber.724028252@node_26400> <1992Dec11.011515.2859@saifr00.cfsat.honeywell.com> <62774@mimsy.umd.edu>
  9. Date: 14 Dec 92 14:49:05 GMT
  10. Lines: 88
  11.  
  12.  
  13. dunbar@saifr00.cfsat.honeywell.com (Mark Dunbar) writes:
  14. > Using Ada83, instead of the nested if statement, one could do the following:
  15. >   if    ( not Cond_1 ) and ( not Cond_2 ) and ( not Cond_3 ) then
  16. >   elsif ( not Cond_1 ) and ( not Cond_2 ) and (     Cond_3 ) then
  17. >   elsif ( not Cond_1 ) and (     Cond_2 ) and ( not Cond_3 ) then
  18. >   etc.
  19.  
  20. alex@cs.umd.edu (Alex Blakemore) writes:
  21. >I've rarely encountered complex nested if statements where
  22. >every combination of conditions had a completely different action.
  23. >More often, several combinations share the same action, or parts of
  24. >the same action.  In such cases, you can frequently improve things
  25. >by combining and reordering tests - often removing a level of nesting.
  26.  
  27. >for a simple example,
  28.  
  29. >  if x then                               if not x then
  30. >    if y then        can be rewritten       C;
  31. >      A;                                  elsif y then
  32. >    else                                    A;
  33. >       B;                                 else
  34. >    end if;                                 B;
  35. >  else                                    end if;
  36. >    C;
  37. >  end if;
  38.  
  39. crigler@cs.ucf.edu (James Crigler) writes:
  40. >Having done this enough times to try to find a solution, I use
  41. >this:  Write a function that takes the conditions shown above as
  42. >inputs and returns a value of an enumerated type, then case on the
  43. >function return value like this:
  44. >
  45. >  type SELECTIONS is (E1, E2, E3);
  46. >
  47. >  function SELECTION (C1, C2, C3 : in BOOLEAN) is
  48. >  begin
  49. >     ...
  50. >  end SELECTION;
  51. >
  52. >  case SELECTION(COND_1, COND_2, COND_3) is
  53. >    when E1 =>
  54. >      STMT1;
  55. >    when E2 =>
  56. >      STMT2;
  57.  
  58. I agree that code equivalent to a multivalued case can be written a number
  59. of ways, but I believe that the multivalued case is a natural extension to
  60. the language.  Mark Dunbar's solution clearly expresses the structure of
  61. the logic (a truth table), but it requires that the conditions be expressed
  62. multiple times or additional variables be declared.  It also does not allow
  63. the compiler to verify that every combination of conditions is uniquely
  64. represented.
  65.  
  66. Alex Blakemore's solution works quite nicely if the problem is simple enough,
  67. but it fails in the large.  Additionaly it sometimes requires convoluted
  68. logic.  The advantage of the multivalued case is that it clearly expresses
  69. the logic of a decision table and works even for very large problems.
  70. This solution also does not provide sufficient information to enable the 
  71. compiler to verify completeness and consistency.
  72.  
  73. As Mr. Blakemore pointed out it is often true that many of the branches share
  74. common actions.  It is my opinion that it is more important the clearly
  75. express the conditions under which each branch is taken than it is to 
  76. express that several branches have the same action.  This is especially
  77. true in long lived code.  Maintenance and design changes often require that
  78. previously identical branches be separated.  This is trivial with the
  79. multivalued case.  It is quite difficult using Mr. Blakemore's approach.
  80.  
  81. I have on ocassion used James Crigler's solution, but only for fairly
  82. large cases involving enumerated types other than boolean.  The implementation
  83. of the Selection function is either identical to the original problem or
  84. an obscure expression involving 'Pos, 'Val, 'First, 'Last, etc.  If the names
  85. of the elements in the enumeration type are well chosen, it can be fairly easy
  86. to read, but establishing its correctness is problematic as it depends on
  87. the rather obscure relationship between the various enumeration types.
  88.  
  89. Let's face it, the only control structures we really need are while and semi-
  90. colon, everything else is just an attempt to simplify the expression of 
  91. common bits of code.  Having been remined by Mssrs. Dunbar, Blakemore,and
  92. Crigler of the various ways of expressing this logic, I am even more certain
  93. that a multivalued case would be a big help.  And it seems to me a simple and 
  94. logical extension to the language.
  95.  
  96. Douglas Surber
  97. Lockheed
  98. Houston, TX
  99.  
  100.