home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / programm / 3099 < prev    next >
Encoding:
Text File  |  1992-11-10  |  4.0 KB  |  86 lines

  1. Newsgroups: comp.programming
  2. Path: sparky!uunet!mcsun!news.funet.fi!network.jyu.fi!sakkinen
  3. From: sakkinen@jyu.fi (Markku Sakkinen)
  4. Subject: Re: Help - What Is Wrong?
  5. Message-ID: <1992Nov10.135848.3730@jyu.fi>
  6. Organization: University of Jyvaskyla, Finland
  7. References: <536@ulogic.UUCP> <544@ulogic.UUCP> <1992Nov9.154402.16285@hubcap.clemson.edu>
  8. Date: Tue, 10 Nov 1992 13:58:48 GMT
  9. Lines: 75
  10.  
  11. In article <1992Nov9.154402.16285@hubcap.clemson.edu> mjs@hubcap.clemson.edu (M. J. Saltzman) writes:
  12. >Discussing a fragment of code of the form 
  13. >
  14. >    for ( i = 0;  i != 5;  i++ )
  15. >        ...
  16. >
  17. >hartman@ulogic.UUCP (Richard M. Hartman) writes:
  18. >>
  19. >>I've always seen (and used) the "i<max" form of the for loop, and
  20. >>messed up when faced w/ anything else.  "i!=max" WILL work, but
  21. >>is less safe for a number of reasons (more so if initial condition
  22. >    ^^^^^^^^^
  23. >>is set by variable instead of literal).
  24. >
  25. >Presumably, "i != 5" is "less safe" because a failure of the initial
  26. >condition to satisfy i < 5 will not result in an infinite loop
  27. >(especially if the initializer "i = 0" is replaced by, say, "i = j + 1",
  28. >or if the initial i is passed as a parameter).  On the other hand,
  29. >this may not be the best kind of "safety" to have.
  30. >
  31. >In _The_Science_of_Programming_[1], David Gries argues that the guards
  32. >on loops should be as weak as possible, so that if the initial
  33. >condition is violated, the program will fail catastrophically (i.e.
  34. >with an infinite loop).  His reasoning is that the failure of i to be
  35. >less than 5 indicates an error elsewhere in the code, which might go
  36. >undetected if the loop fails to execute at all (with the program
  37. >giving a wrong answer).  If the program goes into an infinite loop,
  38. >you can be sure you've stumbled on a bug.
  39.  
  40. Looks like pretty bad advice from such an acknowledged authority.
  41. If catastrophic failure is required, it should be an abort or the like,
  42. not an infinite loop.  Perhaps if the programmer herself is always
  43. tending to her programme when it is run(!), she may reason:
  44. "Far too much time has passed for this stage, it _must_ be stuck
  45. in an infinite loop somewhere."
  46. Infinite loops can be a particularly bad idea in multitasking systems.
  47.  
  48. In this specific example as is, there is not much difference
  49. between the alternatives.  But suppose a little modification:
  50.     for ( i = whiz(x);  i != 5;  i++ ) ...
  51. will be sensible only if you are absolutely sure that 'whiz(x)'
  52. returns a value < 5;  otherwise you should insert an explicit test.  
  53. On the contrary, 'i < 5' works perfectly also in this case,
  54. if the intent is not to execute the loop at all if the value is >= 5.
  55. Also, the code might be modified so that the increment is 2 instead of 1;
  56. 'i < 5' still works in a way that may be sensible.
  57. In general, it looks much more robust than 'i != 5'.
  58.  
  59. >On the other hand, Gries recommends that guards on "if" and "else"
  60. >statements should be as strong as possible, and that the default
  61. >action for any case not explicitly covered in the guards should be to
  62. >abort.  The reasoning is the same: if one of the "if" or "else"
  63. >conditions is not met, there is a failure elsewhere in the program
  64. >that could go undetected if the program is allowed to continue.
  65.  
  66. Obviously you are quoting from memory (see below), and this does
  67. not make any sense.  'If ... then ... else ... endif'
  68. always covers all cases!
  69.  
  70. >[1] This book is a classic, and a very readable introduction to the
  71. >ideas underlying derivation of provably correct programs.  (Sorry, I
  72. >don't have the publisher information handy.)
  73.  
  74. If Gries strongly suggests infinite loops in many places,
  75. the book is evidently aiming at provably _partially_ correct programs. :-)
  76.  
  77. ----------------------------------------------------------------------
  78. Markku Sakkinen (sakkinen@jytko.jyu.fi)
  79.        SAKKINEN@FINJYU.bitnet (alternative network address)
  80. Department of Computer Science and Information Systems
  81. University of Jyvaskyla (a's with umlauts)
  82. PL 35
  83. SF-40351 Jyvaskyla (umlauts again)
  84. Finland
  85. ----------------------------------------------------------------------
  86.