Indentation (was Re: if (e1) return e2; [else] s1;)

Dave Straker daves at hpopd.HP.COM
Wed Mar 28 00:20:26 AEST 1990


>chrisl at caen.engin.umich.edu (Chris Lang) / 11:21 pm  Mar 25, 1990 /
>
>That reminds me to ask, what about this, which is basically my style?
>
>void foo(struct spam *bar)
>    {
>    if (bar->bletch != NULL)
>        {
>        printf("Ve haf a bletch!\n");
>        } /* if */
>    else
>        {
>        bar->bletch = 6;
>        printf("We have one NOW!\n");
>        } /* else */
>    return;
>    } /* foo() */
>
>The point I am most interested in is the indentation of the entire function,
>including opening and closing braces.  Does this strike people as possibly
>confusing?  I find it clearer, and consistant (ie, every sub-block gets
>indented, as do its opening and closing braces).  I feel the final /* foo () */
>makes it obvious that we're at the end of a function, not one level in...
>but what do others feel?

If it helps you, and doesn't upset others who read your code, then it's ok.
Personally, I think it's a waste of an indent to indent the whole function.
Commenting ends of long loops to allow the reader to match it up to the
start is good, especially if the loop falls off the end of a page.
I either start each function on a new page, and/or (v. small functions only)
put a comment bar/block between functions, which makes commenting the end
redundant. Also, if you have nice small functions (< 2 pages, preferably 1)
it's clear enough anyway.

Dave



More information about the Comp.lang.c mailing list