Hubbry Logo
search
logo

Indentation style

logo
Community Hub0 Subscribers
Write something...
Be the first to start a discussion here.
Be the first to start a discussion here.
See all
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.

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 (such as compiler or interpreter). 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 (that delimit blocks with curly brackets, a.k.a. curly braces, a.k.a. braces) 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.

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 (although in that experiment non-indented code just took 113% more time to read) and revealed that the differences in reading times can be explained by the code that can be skipped (for indented code). In another experiment on JSON objects non-indented code took even 544% more time to read.

The table below includes code examples of various indentation styles. For consistency, indentation size for example code is 4 spaces even though this varies by coding convention.

Attributes of C, C++ and other curly-brace programming language coding style include but are not limited to:

See all
User Avatar
No comments yet.