Indentation style
In computer programming, indentation style is a convention or style, governing the indentation of lines of source code. An indentation style generally specifies a consistent number of whitespace characters before each line of a block, so that the lines of code appear to be related, and dictates whether to use spaces or tabs as the indentation character.
Overview
This article primarily addresses styles for free-form programming languages. As the name implies, such language code need not follow an indentation style. Indentation is a secondary notation that is often intended to lower cognitive load for a programmer to understand the structure of the code.Indentation can clarify the separation between the code executed based on control flow.
Structured languages, such as Python and occam, use indentation to determine the structure instead of using braces or keywords; this is termed the off-side rule. In such languages, indentation is meaningful to the language processor. A programmer must conform to the language's indentation rules although may be free to choose indentation size.
This article focuses on curly-bracket languages and in particular
C-family languages, but a convention used for one language can be adapted to another language. For example, a language that uses
BEGIN and END keywords instead of braces can be adapted by treating BEGIN the same as the open brace and so on.Indentation style only applies to text-based languages. Visual programming languages have no indentation.
Research
Despite the ubiquitous use of indentation styles, little research has been conducted on its value. First experiments, conducted by Weissman in 1974, did not show any effect.In 2023, an experiment by Morzeck et al. showed a significant positive effect for nested
if statements where non-indented code required on average 179% more time to read than indented code. A follow up-experiment by Hanenberg et al. confirmed a large effect and revealed that the differences in reading times can be explained by the code that can be skipped. In another experiment on JSON objects non-indented code took even 544% more time to read.Brace placement styles
The table below illustrates how different brace placement styles interact with indentation in control structures.For consistency, indentation size for example code is 4 spaces even though this varies by coding convention.
| Example | Name |
while | [|Allman] |
while | GNU |
while | Whitesmiths |
while | K&R |
while | Ratliff |
while | Horstmann |
while | Pico |
while | Lisp |
| APL |
C/C++ styles
Attributes of C, C++ and other curly-brace programming language coding style include but are not limited to:- Placement of braces relative to other code elements
- Use of tabs or spaces
- Wrapping single-statement blocks in braces. Advocates cite the advantage that resulting code is safer since inserting a statement cannot result in control flow that disagrees with indentation. A cited disadvantage is that the code is longer since one line is needed for the closing brace of a block.
K&R
Although The C Programming Language does not explicitly define this style, it follows it consistently. From the book:
The position of braces is less important, although people hold passionate beliefs. We have chosen one of several popular styles. Pick a style that suits you, then use it consistently.
In this style, a function has its opening and closing braces on their own lines and with the same indentation as the declaration, while the statements in the body of the function are indented an additional level. A multi-statement block inside a function, however, has its opening brace on the same line as its control clause while the closing brace remains on its own line unless followed by a keyword such as
else or while.Example code:
int main
Egyptian braces
The non-aligned braces of the multi-line blocks are nicknamed "Egyptian braces" for their resemblance to arms in some fanciful poses of ancient Egyptians.Single statements
A single-statement block does not have braces, which is a cause of easy-to-miss bugs such as the goto fail bug.One True Brace
The One True Brace Style is a variant of K&R style where braces are not omitted for a single-statement block.bool is_negative
Although not required by languages such as C/C++, using braces for single-statement blocks ensures inserting a new statement does not result in control flow that disagrees with indenting, as seen for example in Apple's infamous goto fail bug.
Some sources disagree as to the meaning of One True Brace Style – it might have slight differences based on most prevalent style for given language, individual authors might declare vastly distinct style as OTBS according to their subjective preferences, while others note it as "hacker jargon" for K&R.
Linux kernel
The Linux kernel source tree is styled in a variant of K&R. Linus Torvalds advises contributors to follow it. Attributes include:- Uses tab characters for indentation and assumes tab stops every 8 spaces
- Brace layout matches K&R, with the braces of function definitions on their own lines and the opening brace of compound statements on the same line as the control clause, separated by a space
- Labels in a
switchstatement are aligned with the enclosing block - Maximum line length is 100 characters although the pre-2020 limit of 80 characters is preferred.
- A single-statement body of a compound statement does not need to be surrounded by curly braces. If, however, one or more of the substatements in an
if-elsestatement require braces, then both substatements should be wrapped in braces:
int power
Java
A significant body of Java code uses a variant of the K&R style in which the opening brace is on the same line not only for the blocks inside a function, but also for class or method declarations.This style is widespread largely because Sun Microsystems's original style guides used this K&R variant, and as a result, most of the standard source code for the Java API is written in this style. It is also a popular indentation style for ActionScript and JavaScript, along with the Allman style.
Stroustrup
adapted the K&R style for C++ in his books, such as Programming: Principles and Practice using C++ and The C++ Programming Language.Unlike the variants above, Stroustrup does not use a "cuddled else". Thus, Stroustrup would write
if
else
Stroustrup extends K&R style for classes, writing them as follows:
class Vector ;
Stroustrup does not indent the labels and. Also, in this style, while the opening brace of a function starts on a new line, the opening brace of a class is on the same line as the class name.
Stroustrup allows writing short functions all on one line. Stroustrup style is a named indentation style available in the editor Emacs. Stroustrup encourages a K&R-derived style layout with C++ as stated in his modern C++ Core Guidelines.
BSD KNF
The Berkeley Software Distribution operating systems uses a style that is sometimes termed kernel normal form. Although mostly intended for kernel code, it is also widely used in userland code. It is essentially a thoroughly documented variant of K&R style as used in the Bell Labs version 6 & 7 Unix source code.The SunOS kernel and userland uses a similar indentation style. Like KNF, this also was based on AT&T style documents and is sometimes termed Bill Joy Normal Form. The SunOS guideline was published in 1996; ANSI C is discussed briefly. The correctness of the indentation of a list of source files can be verified by the cstyle program written by Bill Shannon.
In this style, the hard tabulator is kept at eight columns, while a soft tabulator is often defined as a helper also, and set at four. The hard tabulators are used to indent code blocks, while a soft tabulator of additional indentation is used for all continuing lines that must be split over multiple lines.
Moreover, function calls do not use a space before the parenthesis, although C-language native statements such as
if, while, do, switch and return do. Functions that declare no local variables in their top-level block should also leave an empty line after their opening block brace.Examples:
while
final_thing;
if else
static JSBool
pgresult_constructor