Indent style describes how different programmers use braces to denote blocks of code, most commonly in the C programming language and its descendants, as well as other programming languages that allow indenting. This article concentrates on how indenting is done in C, but can be (and frequently is) applied to any language in the curly brace family.
The reason most programmers indent blocks of code is to clarify their structure and make it easier to read and understand the program. However, some programming languages (such as Python and Occam) use the indentation to determine the structure instead of using braces or keywords.
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 transferred 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 indent styles as well as the length of tabs. A famous one among them is indent, a program included with many Unix-like operating systems. These programs work best for those who use an indent style close to that considered "proper" by their programmers; those who use other styles will more likely become frustrated.
BSD/Allman style
The BSD/Allman style is relatively common. It keeps the braces after the initial statement (for example, 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 each of the enclosing braces occupies an entire line without adding any visible instructions. This once was an important consideration when programs were usually edited on terminals that displayed only 24 lines.
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. People who prefer this style often refer to it as the "One True Brace Style" (abbreviation 1TBS). The source code of the UNIX kernel and Linux kernel is written in this style.
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, and the ending brace shares the disadvantage of the BSD style. The latter can be partially resolved in if/else blocks and do/while blocks:
if (x < 0) {
printf("Negative");
negative(x);
} else {
printf("Positive");
positive(x);
}
However, this also somewhat decreases the readability of the code.
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.
A disadvantage of this style against the BSD style is that the braces do not stand out as well.
GNU style
The GNU style is mixture of BSD and Whitesmiths, with the addition of making a space between the bracketed list of arguments to a function or a construct.
while (x == y)
{
something ();
somethingelse ();
}
This style maintains the advantages of BSD while satisfying those who prefer Whitesmiths. The layout may be influenced by Richard Stallman's background of writing in Lisp. This style is the default behavior of GNU Emacs and is mandated by nearly all maintainers of GNU project software. It is practically exclusive to the GNU project, however, as almost nobody else uses GNU style.
A possible disadvantage is that not all programming editors support this style. Because GNU's roots are firmly in Unix and free software environments, it is unlikely to be supported by tools written specifically for Microsoft Windows.
Another disadvantage is that this style effectively uses two indent levels where one would suffice, leading to formatting inconsistencies if one indents using tabs. While tab stops may be changed, their width is generally constant, and so it is awkward to include this style in another document: either the code excerpt or the enclosing document is likely to become poorly formatted.
Pico style
The style used most commonly in the Pico programming language by its designers, is different from the aforementioned styles. The lack of return statements and the fact that semicolons are used in Pico as statement separators, instead of terminators, leads to the following syntax:
stuff(n):
{ x: 3 * n;
y: doStuff(x);
y + x }
See also