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 came to use 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.


I wrote most of that twenty years ago. Other styles have appeared since then, but developers seem to have settled on K&R; yes, the worst of the above has dominated. One younger teammate said that he'd never seen a style like mine. I don't know whether it's cause or effect, but languages... or perhaps the way that we use them... have evolved in a way that makes legible formatting nearly impossible. I'm often reminded of Lisp when I look at today's code, and how we used to make fun of those parens... Lost In Stupid Parentheses. Well, K&R combined with excessive method chaining makes Lisp look good! And then there's semicolon injection to consider, the only possible justification for K&R. But if Javascript would just stop doing that, and some Googler would fix Go's parser, then we wouldn't need to use such an ugly style. Anyway, I've found that a possible alternative is Ratliff Style; not as logical as Whitesmiths, but not as ugly as K&R...


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

Why don't more languages just use Python Style?

6 Aprilo 1998 de Ailanto kreita, 16 Decembro 2018 modifita.