From: crawford@church.mitre.org (Randy Crawford)
Newsgroups: comp.programming
Subject: Re: Teaching the basics
Date: 27 Aug 92 18:23:09 GMT
Organization: The MITRE Corporation, McLean, VA

In article <1992Aug26.183042.4808@uwm.edu>, markh@csd4.csd.uwm.edu (Mark) writes:
> In article <1992Aug26.172830.21172@organpipe.uug.arizona.edu> dave@cs.arizona.edu (Dave Schaumann) writes:
> >In article <PSU.92Aug26091352@ptero.cs.duke.edu>, psu@cs (Peter Su) writes:
> >>In article <1992Aug26.111240.8239@ITcorp.com> geoff@ITcorp.com (Geoff Kuenning) writes:
> >>
> >>   >...
> >>   >    else if (Prev->Key < Key) Prev->Left = Cur; else Prev->Right = Cur;
> >>
> >>   I find it sourly this code was on a thread named "Teaching the
> >>   basics", since the author obviously hasn't even learned the basics of
> >>   readable code formatting.
> >>
> >>Learn to use indent, or Emacs C-mode.
> >>
> >>Code formatting is irrelevant
> >
> >No it isn't.
> 
> You're right.  It isn't.  And that illustrated (in part) above is not only a
> very readible style of formating -- but in fact one of the most.  Having it
> criticized by someone who even seriously brings up mention of utilities like
> indent, or Emacs only serves to reinforce that assessment of its readibility.

Count me as being unhappy with your C style as well.

compare this:
-------------

Tree Find(int Key) {
   Tree Prev, Cur;
   for (Prev = 0, Cur = Root; Cur != Prev; ) {
      if (Cur->Key == Key) return Cur;
      Prev = Cur, Cur = (Cur->Key < Key)? Cur->Left: Cur->Right;
   }
   Cur = (Tree)malloc(sizeof *Cur);      /* Add in your own error checks */
   Cur->Key = Key, Cur->State = 0;
   Cur->Left = Cur->Right = Cur;
   if (Prev == 0) Root = Cur;
   else if (Prev->Key < Key) Prev->Left = Cur; else Prev->Right = Cur;
   return Cur;
}

with this:
----------

Tree Find( int Key) {
    Tree Prev, Cur;

    Prev = 0; 
    Cur = Root; 
    while (Cur != Prev) { 
        if (Cur->Key == Key) 
            return Cur;

        Prev = Cur;
        if (Cur->Key < Key)  
            Cur = Cur->Left;
        else
            Cur = Cur->Right;
    }

    Cur = (Tree) malloc( sizeof Tree);   /* Add in your own error checks */ 
    Cur->Key   = Key;
    Cur->State = 0;          
 
    Cur->Left = Cur->Right = Cur;

    if (Prev == 0) 
        Root = Cur;
    else if (Prev->Key < Key) 
        Prev->Left = Cur; 
    else 
        Prev->Right = Cur;

    return Cur;
}

The _only_ advantage I see to your format is that it uses fewer lines. 
 
Comma operators are a rotten convention that should never have been intro-
duced in C.  I also dislike the use of the X?Y:Z conditional except in 
macros, and multiple statements per line are unnecessary and confusing.

In addition, your comma operator in:  

   Cur->Key = Key, Cur->State = 0;

should be changed to a semicolon.  The comma is pointless.

Aside from that, I disagree with your opinion and example on recursion too.  
In my book, tree traversal means visiting _every_ node, and that means backing
up the tree.  Try doing that without recursion, a stack or a threaded tree.
-- 

| Randy Crawford        crawford@mitre.org        The MITRE Corporation
|                                                 7525 Colshire Dr., MS Z421
| N=1 -> P=NP           703 883-7940              McLean, VA  22102
Newsgroups: comp.programming