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.

[10] Although not required by languages such as C/C++, using braces for single-statement blocks ensures that inserting a statement does not result in control flow that disagrees with indenting, as seen for example in Apple's infamous goto fail bug.

Attributes include: 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.

Stroustrup encourages a K&R-derived style layout with C++ as stated in his modern C++ Core Guidelines.

[19] The Berkeley Software Distribution (BSD) operating systems uses a style that is sometimes termed kernel normal form (KNF).

It is essentially a thoroughly documented variant of K&R style as used in the Bell Labs version 6 & 7 Unix source code.

[20] Like KNF, this also was based on AT&T style documents and is sometimes termed Bill Joy Normal Form.

The correctness of the indentation of a list of source files can be verified by the cstyle program written by Bill Shannon.

[12] This style is similar to the standard indentation used by the Pascal languages and Transact-SQL, where the braces are equivalent to the keywords begin and end.

For example, the following is still correct syntactically: As is this: Even like this, with conditional compilation: Allman-8 uses the 8-space indentation tabs and 80-column limit of the Linux Kernel variant of K&R.

Also, the indentation size and column restriction help create a visual cue for identifying excessive nesting of code blocks.

These advantages combine to help provide newer developers and learners implicit guidance to manage code complexity.

Whitesmiths, along with Allman, were claimed to have been the most common bracing styles in 1991 by the Jargon File, with roughly equal popularity at the time.

Popularised by Richard Stallman, the layout may be influenced by his background of writing Lisp code.

[27][28][discuss] Although not indentation per se, GNU coding style also includes a space after a function name – before the left parenthesis of an argument list.

[11] The 1997 edition of Computing Concepts with C++ Essentials by Cay S. Horstmann adapts Allman by placing the first statement of a block on the same line as the opening brace.

It yields this syntax:[32] The advantages and disadvantages are similar to those of saving screen real estate with K&R style.

While GNU style is sometimes characterized as C code indented by a Lisp programmer, one might even go so far as to insert closing braces together in the last line of a block.

This style makes indentation the only way to distinguish blocks of code, but has the advantage of containing no uninformative lines.

This is also because, braces aside, Lisp is conventionally a very terse language, omitting even common forms of simple boilerplate code as uninformative, such as the else keyword in an if : then | else block, instead rendering it uniformly as (if expr1 expr2 expr3).

Note: progn is a procedure for evaluating multiple sub-expressions sequentially for effects, while discarding all but the final (nth) return value.

An experiment performed on PASCAL code in 1983, found that indentation size significantly affected comprehensibility.

Typically, a programmer uses a text editor that provides tab stops at fixed intervals (a number of spaces), to assist in maintaining whitespace according to a style.

Storing tab characters in code can cause visual misalignment when viewed in different contexts, which counters the value of the indentation style.

[42] Others, such as the writers of the WordPress coding standards, state the opposite: that hard tabs increase portability.

Emacs provides commands to modify indentation, including hitting Tab on a given line.

This is often experienced in large sections of code containing many compound statements nested to many levels of indentation.

As the programmer scrolls to the bottom of a huge set of nested statements, they may lose track of context – such as the control structure at the top of the block.

Programmers who rely more on indentations will gain more from styles that are vertically compact, such as K&R, because the blocks are shorter.

For example, vi jumps to the brace enclosing the same block as the one under the cursor when pressing the % key.