Braces

Doug Gwyn gwyn at smoke.brl.mil
Thu Feb 7 06:33:10 AEST 1991


In article <2324 at inews.intel.com> bhoughto at pima.intel.com (Blair P. Houghton) writes:
>... I'd like to know where it came from, not the reasons
>or rationalizations for its use:  those I'll get from the
>source.

You seem to be presuming that everybody else using aligned-brace
style is mimicking a style originated by a single source.  It is
much more likely that several minds thinking about the subject
have arrived at similar conclusions for similar reasons.

In my case it originated as an experiment to see if I could find
a code layout that made it easier for me to understand the code
structure.  I tried several variations in C coding style, allowing
enough time to become familiar with each so that I could fairly
judge how each worked in practice.  (When editing existing code,
if it follows a consistent style then I try to adhere to that
style also; consistency is very important.)  My use of whitespace
near punctuators and operators has varied over the years, but at
any given moment I use the style that seems to be the best
compromise between patent structural visibility and the ability
of other programmers to cope with it.

One of the biggest problems with the K&R style
	while ( ... ) {
		...
	}
is that it leads many novice programmers to misunderstand the
function of the braces; they start to think that they are part of
the punctuation for the "while" statement, which is wrong.  I have
a similar objection to a form widely used by Bell Labs programmers:
	return(expression);
which has caused many programmers to think that the parentheses
are required, which is wrong.  While these misconceptions may seem
benign, I have seen a lot of time wasted in explaining to such
misled programmers why
	while ( ... )
		simple_statement;
and
	return expression;
are valid C code.  Styles that reinforce a correct understanding
of the language seem preferable to ones that mislead.



More information about the Comp.lang.c mailing list