In computer programming, an indentation style is a convention governing the indentation of blocks of code to convey program structure. This article largely addresses the free-form languages, such as C and its descendants, but can be (and often is) applied to most other programming languages (especially those in the curly bracket family), where whitespace is otherwise insignificant. Indentation style is only one aspect of programming style.

Indentation is not a requirement of most programming languages, where it is used as secondary notation. Rather, indenting helps better convey the structure of a program to human readers. Especially, it is used to clarify the link between control flow constructs such as conditions or loops, and code contained within and outside of them. However, some 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 compiler or interpreter; it is more than only a clarity or style issue.

Indentation style

This article uses the term brackets to refer to parentheses, and the term braces to refer to curly brackets.

Indentation style

Brace placement in compound statements

The main difference between indentation styles lies in the placing of the braces of the compound statement ({ ...}) that often follows a control statement (if, while, for...). The table below shows this placement for the style of statements discussed in this article; function declaration style is another case. The style for brace placement in statements may differ from the style for brace placement of a function definition. For consistency, the indentation depth has been kept constant at 4 spaces, regardless of the preferred indentation depth of each style.

Indentation style

Tabs, spaces, and size of indentations

The displayed width for tabs can be set to arbitrary values in most programming editors, including Notepad++ (MS-Windows), TextEdit (MacOS/X), Emacs (Unix), vi (Unix), and nano (Unix). In addition, these editors can be configured to generate a mix of tabs and spaces or to convert between tabs and spaces, to match specific indentation schemes. In Unix, the tab width can also be set in pagers, such as less, and converted on the fly by filters, such as expand/unexpand.

Unix editors default to positioning tabs at intervals of eight columns, while Macintosh and MS-Windows environments defaulted to four columns. This difference causes source code misalignment, when indentation that mixes tabs and spaces is displayed under a configuration that displays tabs differently from the author's configuration.

There is ongoing debate amongst programmers about the choice between hard tabs and spaces. Many early programmers used tab characters to indent, for ease of typing and to save on source file size. Some programmers, such as Jamie Zawinski, state that using spaces instead of tabs increases cross-platform portability. Others, such as the writers of the WordPress coding standards, state the opposite: that hard tabs increase portability. A survey of the top 400,000 repositories on GitHub found that spaces are more common.

The size of the indentation is usually independent of the style. An experiment performed on PASCAL code in 1983, found that indentation size significantly affected comprehensibility. Indentation sizes between 2 and 4 characters proved optimal. For Ruby, many shell scripting languages, and some forms of HTML formatting, two spaces per indentation level is generally used.

Tools

There are many tools to convert between indentation styles, such as indent, a program included with many Unix-like operating systems.

In Emacs, various commands are available to automatically fix indentation problems, including hitting Tab on a given line (in the default configuration). M-x indent-region can be used to properly indent large sections of code. Depending on the mode, Emacs can also replace leading indentation spaces with the proper number of tabs followed by spaces, which results in the minimal number of characters for indenting each source line.

Elastic tabstops is a tabulation style which requires support from the text editor, where entire blocks of text are kept automatically aligned when the length of one line in the block changes.