Main Page | See live article | Alphabetical index

Indent style

Indent style describes how different programmers use braces to denote blocks of code, most commonly in the C programming language, as well as other programming languages that allow indenting. This article concentrates on how indenting is done in C.

The reason most programmers indent blocks of code is to clarify their structure and make it easier to read and understand the program.

The size of the indent is somewhat independent of the style. Many early programs used tab characters for indentation, for simplicity and to save on source file size. Unix peripherals would generally have tabs equivalent to eight characters, while Macintosh environments would set them to four, creating confusion when code was transfered back and forth. Modern programming editors are now often able to set arbitrary indentation sizes, and will insert the appropriate combination of spaces and tabs. They can also help with the cross-platform tab confusion by being configured to insert only spaces.

There are a number of computer programs that automatically correct ident styles as well as the length of tabs. Among them a famous one is indent, a unix program.

Table of contents
1 BSD/Allman style
2 K&R style
3 Whitesmiths style
4 GNU style
5 Other styles
6 External link

BSD/Allman style

The BSD/Allman style is relatively common. It keeps the braces after the initial statement (ie for a while loop), and tabs out the blocked statements.

while(x == y)
{
    something();
    somethingelse();
}

The advantages of this style are that the indented code is clearly set apart from the containing statement by lines that are almost completely whitespace; the braces line up with the statement they conceptually belong to; and the ending brace lines up with the beginning brace.

The disadvantage of this style is that the beginning brace occupies an entire line without adding any functionality to the code. This once was an important consideration when programs were usually edited on terminals that displayed only 24 lines at once.

K&R style

The K&R style, so-called because it was used in Kernighan and Ritchie's book The C Programming Language, is somewhat common. It keeps the first opening brace on the same level as the initial statement, while using the closing brace to denote the block's end.

while(x == y) {
    something();
    somethingelse();
}

The advantages of this style are that the ending brace lines up with the statement it conceptually belongs to; and the beginning brace does not occupy an entire line by itself.

The disadvantage of this style is that the beginning brace can be more difficult to locate than with the BSD style.

Whitesmiths style

The Whitesmiths style is somewhat like the BSD style:

 
while(x == y)
    {
    something();
    somethingelse();
    }

The advantages of this style are similar to those of the BSD style. Some advocates of this style will point out that strictly speaking the braces combine a series of declarations and statements into a single statement. Therefore, they should be indented as subordinate to the while instead of lined up with it.

The disadvantage of this style is that the braces do not stand out as well as with the BSD style.

GNU style

The GNU style is mixture of BSD and Whitesmiths.

while(x == y)
  {
    something();
    somethingelse();
  }

This style maintains the advantages of BSD while satisfying those who prefer Whitesmiths.

A possible disadvantage of this style is that deep nesting will cause lines to start very far to the right, possibly causing the lines to wrap around and making them harder to read, but this is ameliorated by indenting the braces by only 2 spaces and the body by another 2, with a net effect no worse than the other styles. This style is the default behavior of GNU Emacs, and is mandated by nearly all maintainers of GNU project software.

Other styles

Other styles may be seen from time to time, usually the brainchild of an individual or group that never caught on.

External link