home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / programm / 3115 < prev    next >
Encoding:
Internet Message Format  |  1992-11-11  |  1.4 KB

  1. Path: sparky!uunet!not-for-mail
  2. From: avg@rodan.UU.NET (Vadim Antonov)
  3. Newsgroups: comp.programming
  4. Subject: Re: Help - What Is Wrong?
  5. Date: 11 Nov 1992 16:04:17 -0500
  6. Organization: Berkeley Software Design
  7. Lines: 45
  8. Message-ID: <1drsghINN9la@rodan.UU.NET>
  9. References: <1992Nov9.154402.16285@hubcap.clemson.edu> <1992Nov10.135848.3730@jyu.fi> <1992Nov11.194809.18616@hubcap.clemson.edu>
  10. NNTP-Posting-Host: rodan.uu.net
  11.  
  12. Well, both advices (i.e. to have strict guards in loops instead of
  13. weak ones and to use if-s covering all cases) should be taken with
  14. a LARGE grain of salt.
  15.  
  16. First of all, never write for( x = 0.0 ; x != 1.0 ; x += 0.1 ) ...
  17.  
  18. Then, use assert-s.
  19.  
  20. Instead of relying on infinite loops (and given modern CPUs
  21. 32-bit "infinite loop" may be under a second) which can
  22. pass unnoticed (or worse...) you may write
  23.  
  24.     assert(i <= n);
  25.     for( ; i < n ; i++ )
  26.         ....
  27.  
  28. In case of ifs
  29.  
  30.     if( cond1 ) {
  31.         ...
  32.     } else if( cond2 ) {
  33.         ...
  34.     } else {
  35.         assert(cond3);
  36.         ...
  37.     }
  38.  
  39. will do essentially the same thing as suggested
  40.  
  41.     if( cond1 ) {
  42.         ...
  43.     } else if( cond2 ) {
  44.         ...
  45.     } else if( cond3 ) {
  46.         ...
  47.     } else
  48.         abort();
  49.  
  50. but will print out additional information making the search easier
  51. (it's especially useful if the stack was trashed so debugger can't
  52. reconstruct the sequence of calls) and the program can be later
  53. recompiled with null assert-s to eliminate the performance penalty
  54. for extra checks.
  55.  
  56. --vadim
  57.