[comp.lang.c] Braces

gwyn@smoke.brl.mil (Doug Gwyn) (02/07/91)

In article <2324@inews.intel.com> bhoughto@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.

gvr@cs.brown.edu (George V. Reilly) (02/07/91)

In article <2324@inews.intel.com> bhoughto@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.

The jargon file, version 2.5.1, has this to say on the matter of
indent styles:

<indent style> [C programmers] n. The rules one uses to lay out code
   in a readable fashion; a subject of <holy wars>.  There are four
   major C indent styles, as described below; all have the aim of
   making it easier for the reader to visually track the scope of
   control constructs.  The significant variable is the placement of
   { and } with respect to the statement(s) they enclose and the
   guard (if, while, or do) on the block, if any.

   "K&R style" --- Named after Kernighan & Ritchie, because the
   examples in <K&R> are formatted this way. Also called "kernel
   style" because the UNIX kernel is written in it.  The basic indent
   shown here is 8 spaces (or 1 tab) per level; 4 is occasionally seen
   but much less common.

     if (cond) {
             <body>
     }

   "Allman style" --- Named for Eric Allman, a Berkeley hacker who
   wrote a lot of the BSD utilities in it (it is sometimes called
   "BSD style"). Resembles normal indent style in Pascal and
   Algol.  Basic indent per level shown here is 8 spaces, but 4 is
   just as common (esp.  in C++ code).

     if (cond)
     {
             <body>
     }

   "Whitesmiths style" --- popularized by the examples that came
   with Whitesmiths C, an early commercial C compiler. Basic indent
   per level shown here is 8 spaces, but 4 is occasionally seen.

     if (cond)
             {
             <body>
             }

   "GNU style" --- Used throughout GNU EMACS and the Free Software
   Foundation code, and just about nowhere else. Indents are always 4
   spaces per level, with { and } "centered" between levels.

     if (cond)
       {
         <body>
       }

   What style one uses is very much a matter of personal choice, but
   one should be consistent within any one software package.
   Statistically, surveys have shown the Allman and Whitesmiths styles
   to be the most common, with about equal `mind share'.  K&R used to
   be nearly universal, but is now much less common (the opening brace
   tends to get lost against the right paren of the guard part in an
   if or while, which is a Bad Thing).
________________
George V. Reilly   `Brace yerself, Bridget'  gvr@cs.brown.edu  +1(401)863-7684
uunet!brunix!gvr   gvr@browncs.bitnet	Box 1910, Brown U, Prov, RI 02912

adrian@mti.mti.com (Adrian McCarthy) (02/08/91)

In article <64002@brunix.UUCP> gvr@cs.brown.edu (George V. Reilly) writes:
>   "K&R style" --- Named after Kernighan & Ritchie....   The basic indent
>   shown here is 8 spaces (or 1 tab) per level; 4 is occasionally seen
>   but much less common.

``Much less common?''  My K&R book uses 4-space indentations exclusively.
:-)

Aid.  (adrian@gonzo.mti.com)

bhoughto@hopi.intel.com (Blair P. Houghton) (02/08/91)

In article <64002@brunix.UUCP> gvr@cs.brown.edu (George V. Reilly) writes:
>The jargon file, version 2.5.1, has this to say on the matter of
>indent styles:

Thanks, George.  I knew there'd be an authoritative answer, somewhere.
(Gotta uncompress that jargon directory...)

>   "Whitesmiths style" --- popularized by the examples that came
>   with Whitesmiths C, an early commercial C compiler. Basic indent
>   per level shown here is 8 spaces, but 4 is occasionally seen.
>     if (cond)
>             {
>             <body>
>             }

Bingo.  Whitesmiths.  PC-land.  When I get my HP "Jaguar"
and need an 8088-based C, maybe I'll do it, too.

				--Blair
				  "'Nuff sed."

gwyn@smoke.brl.mil (Doug Gwyn) (02/08/91)

In article <2360@inews.intel.com> bhoughto@hopi.intel.com (Blair P. Houghton) writes:
>Bingo.  Whitesmiths.  PC-land.

Hardly.  The initial Whitesmiths target was the PDP-11 under DEC OSes;
in the second batch of products targets included DEC VAX and MC68000.

The style of C indentation obviously has nothing to do with machine
architecture.