Sola Ĝusta Krampo-Stilo


The Jargon File has this to say about Brace Styles:

:indent style: /n./ [C programmers] The rules one uses to indent code in a readable fashion. There are four major C indent styles, 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 to the guard or controlling statement (`if', `else', `for', `while', or `do') on the block, if any.

And I, this...

Consider if. The basic form is:


   if (condition) statement;

A statement is either a simple statement or a compound statement. A compound statement is defined as a block of statements grouped together by braces. Thus the braces are part of the compound statement, not part of the guard or controlling statement— the braces are paired, one opening and one closing, and so should be at the same level of indentation— and the indentation of the braces should match that of the statements in the block, giving...


   if (condition)
      {
      statement1;                      Whitesmiths Style
      statement2;
      }

That's the style that I evolved after many years of coding. It is the One True Brace Style, logical and clear, and I found out later that it has a name: it is known as Whitesmiths Style, because it was the style used in examples that came with Whitesmiths C, an early commercial C compiler. A similar style is GNU Style, used by the GNU Project and the Free Software Foundation. Indents are always four spaces per level, with the braces halfway between the outer and inner indent levels...


   if (condition)
     {
       statement1;                     GNU Style
       statement2;
     }

I see no reason for the extra indentation of the statements in the block, but it does no harm. BSD Style, or Allman Style, pushes the braces further left, to the indentation level of the guard or controlling statement, and this is a Bad Thing, because it appears that the braces are part of the guard or controlling statement; they are not. But at least it's still fairly easy to read, and the paired braces are indented to the same level...


   if (condition)
   {
       statement1;                     BSD/Allman Style
       statement2;
   }

And finally we come to the worst of the four major styles, the K&R Style, named after Kernighan and Ritchie, because the examples in The C Programming Language use it, and also called Kernel Style, because the Unix kernel is written in it. The opening brace can be hard to find on the far right side of the controlling statement, or off the screen if your editor isn't wrapping— the braces themselves have different levels of indentation (that of the opening brace being indeterminate!)— indentation of the closing brace matches that of the control statement instead of the statements which it groups— it's about as wrong as it could be...


   if (condition) {
       statement1;                     K&R/Kernel Style
       statement2;
   }

(But it's interesting to note that Kernighan and Ritchie use BSD Style bracing for function bodies!)

According to the Jargon File, surveys have shown that the BSD and Whitesmiths styles are the most commonly used.

More info may be found at Wikipedia's Indent style page.

6 Aprilo 1998 de Ailanto kreita, 30 Oktobro 2016 modifita.