home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / programm / 2508 < prev    next >
Encoding:
Internet Message Format  |  1992-08-27  |  3.6 KB

  1. Path: sparky!uunet!dtix!darwin.sura.net!zaphod.mps.ohio-state.edu!rpi!think.com!linus!linus.mitre.org!crawford
  2. From: crawford@church.mitre.org (Randy Crawford)
  3. Newsgroups: comp.programming
  4. Subject: Re: Teaching the basics
  5. Message-ID: <1992Aug27.182309.3795@linus.mitre.org>
  6. Date: 27 Aug 92 18:23:09 GMT
  7. References: <1992Aug26.111240.8239@ITcorp.com> <PSU.92Aug26091352@ptero.cs.duke.edu> <1992Aug26.172830.21172@organpipe.uug.arizona.edu> <1992Aug26.183042.4808@uwm.edu>
  8. Sender: crawford@church (Randy Crawford)
  9. Organization: The MITRE Corporation, McLean, VA
  10. Lines: 97
  11. Nntp-Posting-Host: church.mitre.org
  12.  
  13. In article <1992Aug26.183042.4808@uwm.edu>, markh@csd4.csd.uwm.edu (Mark) writes:
  14. > In article <1992Aug26.172830.21172@organpipe.uug.arizona.edu> dave@cs.arizona.edu (Dave Schaumann) writes:
  15. > >In article <PSU.92Aug26091352@ptero.cs.duke.edu>, psu@cs (Peter Su) writes:
  16. > >>In article <1992Aug26.111240.8239@ITcorp.com> geoff@ITcorp.com (Geoff Kuenning) writes:
  17. > >>
  18. > >>   >...
  19. > >>   >    else if (Prev->Key < Key) Prev->Left = Cur; else Prev->Right = Cur;
  20. > >>
  21. > >>   I find it sourly this code was on a thread named "Teaching the
  22. > >>   basics", since the author obviously hasn't even learned the basics of
  23. > >>   readable code formatting.
  24. > >>
  25. > >>Learn to use indent, or Emacs C-mode.
  26. > >>
  27. > >>Code formatting is irrelevant
  28. > >
  29. > >No it isn't.
  30. > You're right.  It isn't.  And that illustrated (in part) above is not only a
  31. > very readible style of formating -- but in fact one of the most.  Having it
  32. > criticized by someone who even seriously brings up mention of utilities like
  33. > indent, or Emacs only serves to reinforce that assessment of its readibility.
  34.  
  35. Count me as being unhappy with your C style as well.
  36.  
  37. compare this:
  38. -------------
  39.  
  40. Tree Find(int Key) {
  41.    Tree Prev, Cur;
  42.    for (Prev = 0, Cur = Root; Cur != Prev; ) {
  43.       if (Cur->Key == Key) return Cur;
  44.       Prev = Cur, Cur = (Cur->Key < Key)? Cur->Left: Cur->Right;
  45.    }
  46.    Cur = (Tree)malloc(sizeof *Cur);      /* Add in your own error checks */
  47.    Cur->Key = Key, Cur->State = 0;
  48.    Cur->Left = Cur->Right = Cur;
  49.    if (Prev == 0) Root = Cur;
  50.    else if (Prev->Key < Key) Prev->Left = Cur; else Prev->Right = Cur;
  51.    return Cur;
  52. }
  53.  
  54. with this:
  55. ----------
  56.  
  57. Tree Find( int Key) {
  58.     Tree Prev, Cur;
  59.  
  60.     Prev = 0; 
  61.     Cur = Root; 
  62.     while (Cur != Prev) { 
  63.         if (Cur->Key == Key) 
  64.             return Cur;
  65.  
  66.         Prev = Cur;
  67.         if (Cur->Key < Key)  
  68.             Cur = Cur->Left;
  69.         else
  70.             Cur = Cur->Right;
  71.     }
  72.  
  73.     Cur = (Tree) malloc( sizeof Tree);   /* Add in your own error checks */ 
  74.     Cur->Key   = Key;
  75.     Cur->State = 0;          
  76.  
  77.     Cur->Left = Cur->Right = Cur;
  78.  
  79.     if (Prev == 0) 
  80.         Root = Cur;
  81.     else if (Prev->Key < Key) 
  82.         Prev->Left = Cur; 
  83.     else 
  84.         Prev->Right = Cur;
  85.  
  86.     return Cur;
  87. }
  88.  
  89. The _only_ advantage I see to your format is that it uses fewer lines. 
  90.  
  91. Comma operators are a rotten convention that should never have been intro-
  92. duced in C.  I also dislike the use of the X?Y:Z conditional except in 
  93. macros, and multiple statements per line are unnecessary and confusing.
  94.  
  95. In addition, your comma operator in:  
  96.  
  97.    Cur->Key = Key, Cur->State = 0;
  98.  
  99. should be changed to a semicolon.  The comma is pointless.
  100.  
  101. Aside from that, I disagree with your opinion and example on recursion too.  
  102. In my book, tree traversal means visiting _every_ node, and that means backing
  103. up the tree.  Try doing that without recursion, a stack or a threaded tree.
  104. -- 
  105.  
  106. | Randy Crawford        crawford@mitre.org        The MITRE Corporation
  107. |                                                 7525 Colshire Dr., MS Z421
  108. | N=1 -> P=NP           703 883-7940              McLean, VA  22102
  109.