Indentation in Program Code

Lecture



Indentation — a way of formatting program code using indents so that structural blocks are clearly visible. Why indentation is needed:

  • when reading code, to determine the sequence of instruction execution at a glance and understand under which conditions a given instruction will be executed,
  • when writing code, to avoid getting confused by the nesting levels of blocks.

In addition, there are certain programming languages (Python and several languages derived from it) where indentation is mandatory and directly affects program execution. In most programming languages indentation is not mandatory, however it is strongly recommended by all guides on writing program code.

Indentation style — the rules for formatting source code, according to which the indents of program blocks are placed in a readable manner.

The indentation style being used is usually specified separately in the code formatting standard.

The text editors included in most popular development environments often provide tools to support the indentation style being used, for example, automatic insertion of spaces/tabs when typing the braces that mark the beginning/end of a logical block.

Indentation in Program Code

Example of indentation style settings in PhpStorm

Indentation in C

There are four main indentation styles in C. Described below, they all aim to make it easier to visually track control structures. The point of contention is the placement of the braces { and } and the arrangement of statements (if, else, for, while, or do) into blocks.

«K&R» style

Named after Kernighan and Ritchie because all the examples from their book «The C Programming Language» (often referred to simply as «K&R» by the authors' initials) are formatted in this way. Also known as «kernel style» (BSD KNF; because the UNIX kernel is written in it), and also as the «One True Brace Style» (1TBS) according to its adherents. The main indent, shown below, consists of 8 spaces (or one tab) per level. Although 4 spaces are most often used.

 Indentation in Program Code

Allman style

Allman style — named after Eric Allman, a programmer from the University of Berkeley, who wrote many BSD utilities in it (also known as «BSD style»). It has similarities with Pascal and Algol. This style is offered by default in Microsoft Visual Studio.

The style is as follows:

  • The opening brace is placed on a new line with the same indent as the expression on the preceding line.
  • The first expression inside the braces is placed on a new line with the indent increased (at the programmer's choice) by:
    • 1 tab character (this variant is used in the source code of modern versions of the sendmail program, authored by Eric Allman)
    • 2, 4, or 8 spaces (the specific choice of the number of spaces must remain unchanged throughout the entire program text)
    • any other number of spaces or tab characters, provided that this number does not change throughout the entire program text
  • Subsequent expressions inside the braces are placed with the same indent as the first.
  • The closing brace is placed with an indent equal to that of its corresponding opening brace (that is, exactly below it).

An example of code formatted in Allman style:

Indentation in Program Code

Whitesmiths style

Whitesmiths style — popular because of the examples that came with Whitesmiths C — one of the first compilers of the C language. The main indent per level for braces and the block — 4 spaces.

Indentation in Program Code

GNU style

GNU style — used in all sources of the GNU project (for example, GNU Emacs). The indent is 2 characters per level, and the braces are placed at their own indent .

 Indentation in Program Code

See also

  • Code formatting standard
  • Syntax highlighting
  • [[b8477]]

See also

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Software and information systems development"

Terms: Software and information systems development