why no "do : until"?

Samuel A. Falvo II kc5tja at garnet.armored.net
Tue Jan 2 20:41:28 EST 2001


On Tue, 02 Jan 2001 21:19:19 GMT, Grant Edwards wrote:
>>while (1)
>>   {
>>   T = f();
>>   if (T < 25)
>>       break;
>>   }
>>
>>Use of that convention is probably what has made me quite happy with
>>Python's indentation style, which essentially matches this pattern.
>
>I used that C indentation style for years, but I finally gave
>up since a) I could never get editors and the "indent" program
>to do it automatically, and b) nobody else on my projects
>wanted to do it that way, and it's important that all the
>source code on a project be indented consistently.

That method is almost always self-inconsistent as well.  Most of the people
I see using that technique writes code like this:

void f( int c )
{
   int x = 0;
   
   while( x < c )
      {
      printf( "X=%d\n", x );
      x++;
      }
}

Note that indentation occurs at the outer-most scope only; beyond that, the
"method 3" approach is used.

To make it self-consistent, you need to do this:

void f2( int c )
   {
   int x = 0;
   
   while( x < c )
      {
      printf( "X=%d\n", x );
      x++;
      }
   }

Though both methods are equally readable to me, I consider it to be rather
ugly syntax.  For me, braces represent the start and end of the containing
scope of the element before (in this case the while statement).  As such,
they are part of the *while* statement, not of the contained code.  I like
to think of the braces as a box within which I put statements.  The box is a
logically separate entity for me -- a quantifiable container that is really
quite independent of the contents, yet still allowing me to treat the
interior contents as a cohesive whole, as any cardboard box would be.  Thus,
I'd write the above as follows:

int f3( int c )
{
   int x = 0;
   
   while( x < c )
   {			/* <-- the while "box" */
      printf( "X=%d\n", x );
      x++;
   }
}

It also provides a sense of visual closure, which is a great help to me when
debugging.

However, the other view can also be considered -- the braces envelop a
sequence of code to be treated as a single, logical statement, which of
course statements like while() accept.  Thus, indenting them, along with the
contained statements, also makes sense *if* you think of them in this way.

There's really no right or wrong way -- it's all just a matter of personal
preference.  However, I suspect that debugging habits and commonly found
typographical errors in the source code for large projects is what led many
people to adopt their respective methods of indentation.  It all depends on
the task at hand, I guess.

-- 
KC5TJA/6, DM13, QRP-L #1447 | Official Channel Saint, *Team Amiga*
Samuel A. Falvo II	    |
Oceanside, CA		    |



More information about the Python-list mailing list