Standard Indentation etc.

Geoff Clemm geoff at endor.harvard.edu
Thu Dec 15 05:18:25 AEST 1988


In article <663 at htsa.uucp> fransvo at htsa.UUCP (Frans van Otten) writes:
>I asked Dennis Ritchies opinion on this matter.
>Answering me, he wrote:
>
>  " I like the style used in the C book and in many Unix programs because
>    I regard the {} as noise that is (for C) necessary; the indentation
>    should show the grouping.  Also, I dislike unnecessary vertical space
>    because it makes programs too hard to see at once.  Finally it is
>    desired to edit programs conveniently.  [reasons why omitted]
>    
>    Else  is placed below  if  because it is equally important; the two
>    (or more, as in  else if ) branches are not all under control of the
>    if  but are alternatives.
>    [disclaimer about personal style omitted]

As is to be expected, Ritchie's comments are the most sensible thing I
have seen on this issue (thanks for posting them, Frans !).

To emphasize
  1. {} ARE NOISE FOR THE COMPILER - humans can see in 2d, and indentation
     blocks give you everything that you need.
  2. If you want white space, PUT IT IN ... it is absurd to blither about
     how you need the {}'s to enforce white space.  The space is even
     whiter if there are no {}'s there.
  3. Editing convenience is then the determining factor for how you place
     your {}'s - put them in a way that minimizes numbers of keystrokes
     and errors during creation and modification of code.
  
>Except the placing of the {} and the if-else I agree with this.

The if-else is a separate issue (where Ritchie is also correct), but
if you don't agree with the {} placement, you haven't followed what
he was saying.

>  while (1)         while (1){
>    { first;                first;
>      second();             second();
>      etc;                  etc;
>    }               }

You have sacrificed editing convenience (i.e. being able to kill the line
containing "first;" without messing with brackets) in order to get bracket
column matching, which as Ritchie points out, is irrelevant.

If the compiler could read white space, you would just write :

 while (1)
    first;
    second();
    etc;

As it is, you need to add brackets.  The only place for the open bracket
is on the first line, and you have two choices for the closing bracket
(other than Ritchie's choice) :

 while (1) {			while (1) {
    first;			   first;
    second();			   second();
    etc; }/*while*/;		   etc;
				   }/*while*/;

Note : the brackets DO NOT LINE UP, and it DOESN'T MATTER.  Brackets are
for the compiler, and it doesn't care where they are.  Indentation
blocks are for humans, and this bracket placement emphasizes the correct
2d blocks.

(Commenting all closing brackets is my personal style ... I recommend it.)

Disclaimer : Like all sensible people, I agree that the important point
is to use a common style - what that style happesn to be is secondary.

Geoffrey Clemm
geoff at harvard.harvard.edu



More information about the Comp.lang.c mailing list