Why?? (prog question)

William Gordon Rutherdale will.rutherdale at utoronto.ca
Wed Apr 1 05:11:07 PDT 2009


Oliver Fromme wrote
> Of course this is purely a matter of taste and personal
> preference.  My preference is similar to yours, but my
> main reasoon is to save space.  I think it is a ridiculous
> waste of space if every third line consisted only of a
> sole brace (opening or closing).  To my eye, such lines
> that are almost empty break the natural structure of a
> piece of source code.  I insert empty lines quite often
> in order to group source lines logically, but brace lines
> break that grouping visually.
>
>   
There is a very logical reason in C for wanting to put the opening brace 
of an 'if' statement on a separate line:  preprocessor statements.

Many editors, including vi / vim, and no doubt emacs, have a brace 
matching facility.  If I put the cursor over a closing brace, say, and 
hit the % key, it puts me onto the matching opening brace.  However in 
this scenario:

int foo( int x )
{
#ifdef SCENARIO_A
    if ( x<3 ) {
#else
    if ( x<2 ) {
#endif
        // . . .
    }
    // . . .
}

matching the closing brace of foo() will fail, as the number of braces 
no longer matches.

Putting the opening brace of the 'if' on another line solves this problem.

-Will



More information about the freebsd-questions mailing list