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 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.
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.
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 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 , 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.
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.
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 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