Imposed indentation standards don't work

Eric S. Raymond eric at snark.UUCP
Thu Dec 22 12:40:53 AEST 1988


The two most important thing to remember about style standards are:

1. Their payoff is in inverse proportion to the shop's average skill level,

and

2. Style standards imposed from above don't work and breed resentment.

I wrote most of a book on portable C coding once. We deliberately avoided
issuing any fiats about indentation beyond "be consistent!", knowing they
would be a pointless and losing proposition.

That having been said, I will now shamelessly plug for *my* one personal
style crotchet ;-). Religious flames will be ignored but I am genuinely
interested in what y'all think of my aesthetic arguments.

In general, I code in what I think of as "BSD/Allman" style with 4-space
indent:

int samplefunc(arg1, arg2)
char    *arg1;	/* args start after a tab column */
int     arg1;
{
    int foo;          /* a blank line separates auto declares from code */

    if (ifcond)       /* extra spaces around conditionals annoy me */
    {
        for (i = startval; i < endval; i++)    /* note space after ; */
        {
	    switch (val)        /* indent cases at same level as switch */
            {
            case VAL1:
                c = b + a;     /* note spaces around = and + */
                break;
            case VAL2:
                function(arg1, arg2);          /* no space before ( */
                break;
            default:
                break;
            }
        }
    }
}

Simple, clean, and editor-friendly. The fact that I was a LISP and Pascal
hack before I learned C is not irrelevant :-). Here's my one crotchet: I
like to write

do {
    /* interior-stuff */
} while
    (condition);

Why? Well, first of all, I strongly dislike do-layouts that crowd the condition
up against code in the do-scope, like:

do {
   /* if there's lots of busy stuff */
   /* happening inside the do block */
} while (the condition is hard to read)

Compare this with

do {
   /* if there's lots of busy stuff */
   /* happening inside the do block */
} while
   (isn't this easier?)	    /* visually distinguish the condition part */

So the condition should be separated by one line from the block. The
reasonable choices for end-of-do-block layout are then

    /* #1 */
}
while (cond)

or

    /* #2 */
} while
   (cond);

or

    /* stuff */
}
while
    (cond);

#3 strikes me as too much of a good thing. It would imply a start-of-block
layout like:

do
{
    /* A */

which means I clobber 6 lines of vertical space in overhead for this one
construct -- no thanks, I've only got a 23-line screen!

#1 has the problem that the (cond) part doesn't fall on a 'natural' indent
level; everything else is at 4n stops, but it's at 4n + 7. Ugly. So I go
with #2, and I think that pretty much demands a 'do {' by symmetry.
-- 
      Eric S. Raymond                     (the mad mastermind of TMN-Netnews)
      Email: eric at snark.uu.net                       CompuServe: [72037,2306]
      Post: 22 S. Warren Avenue, Malvern, PA 19355      Phone: (215)-296-5718



More information about the Comp.unix.wizards mailing list