Indentation (typesetting) explained

__FORCETOC__

In the written form of many languages, indentation describes empty space, a.k.a. white space, used around text to signify an important aspect of the text such as:

Many computer languages use block indentation to demarcate blocks of source code.

Indention is essentially the same regardless of whether the writing system is left-to-right (e.g. Latin and Cyrillic) or right-to-left (e.g. Hebrew and Arabic) when considering line beginning and end. For example, indenting at the beginning of line means on the left for a left-to-right script and on the right for right-to-left script.

Indent is both a noun and a verb. The verb is the act of formatting text to be indented whereas the noun refers to the resulting empty space.

Types

There are three main types of indentation: first-line, hanging and block.

Each example below is in a box that represents the page boundary and uses the common typesetting lorem ipsum content. The width of indentation here is in units of em spaces.

Indentation in programming

See also: Indentation style.

In computer programming, indentation describes formatting source code with whitespace to the left of code text often to visually show that a sequence of code lines is syntactically a code block. Typically, the lines of a block are aligned with an amount of white space that indicates the block's depth in the hierarchical structure of the code. Each inner level of the hierarchy is indented by a multiple of this indentation width.

White space in code is typically stored as whitespace characters.

For a free-form language, indentation is exclusively for the programmer since a code processor (i.e. compiler, interpreter) ignores whitespace characters. Code can have inconsistent or even no indentation, but in general is formatted with somewhat consistent indentation.

Some languages rely on indentation to demarcate block structure, often via the off-side rule. Due to this syntax requirement, the code must have a level of consistency that is not required in free-form language code.

The neologisms outdent, unindent and dedent describe the opposite of indentation aligning code text of a line to the left of the previous line.

Variations

Common variations in the implementation of indentation include: how much to indent a block at each level of the code hierarchy, usually measured in spaces, and whether to store whitespace characters as space or tab characters.Although there are common practices, consensus is not universal.[1] These variations are driven by factors that may include but are not limited to: language syntax, organizational mandate and personal preference.

Common practices

The following table identifies notable practices with respect to code indentation.

Language Free-form? Note
Fritz Mehner's style guide suggests 2, 4, or 8 spaces and uses 2 in all examples[2]

Google uses 2 spaces[3]

The Linux kernel uses 1 tab[4] [5]

NASA uses 4 spaces.[6]

Clinton Staley advocates 3 spaces[7]

WebKit recommends 4 spaces[8] Google uses 2 spaces[9]
Microsoft convention: 4 spaces[10]
Drupal, GitHub, and Google use 2 spaces[11] [12] [13] [14]

WordPress uses tabs[15]

dart format uses 2 spaces
Delphi style guide: 2 spaces [16]
F# style guide: 4 spaces[17]
Effective Go: 1 tab [18]
Google uses 2 spaces[19]
Google uses 2 spaces

HTML Tidy defaults to 2 spaces[20]

Oracle uses 4 spaces[21]

Android uses 4 spaces[22]

Most Eclipse IDE components use tabs

Douglas Crockford advocates 4 spaces[23]

GitHub and Google use 2 spaces[24] [25]

jQuery uses tabs[26]

Firefox's built-in jsbeautifier defaults to 2 spaces

prettyprinter in Google Chrome and Internet Explorer use 4 spaces

JuliaJulia style guide: 4 spaces[27]
KotlinKotlin style guide: 4 spaces[28]
Lua style guide: 2 spaces[29]
Larry Wall prefers 4 spaces[30]
Drupal use 2 spaces[31]

PEAR and Zend use 4 spaces[32] [33]

CodeIgniter and WordPress use tabs[34] [35]

PSR-2 specifies 4 spaces[36]

Unofficial PowerShell Best Practices and Style Guide: 4 spaces[37]
PEP 8: 4 spaces[38]
Ruby style guide: 2 spaces [39]
Rust style guide: 4 spaces [40]
Scala style guide: 2 spaces [41]
Google uses 2 spaces[42]
Tcl style guide: 4 spaces [43]
Microsoft convention: 4 spaces [44]
Google uses 2 spaces[45]

Elastic tabstops

In 2006, a new method of indentation was proposed, called elastic tabstops.

Notes and References

  1. Web site: Tabs versus Spaces: An Eternal Holy War . Jwz.org . 2007-01-05 . 2014-01-18.
  2. Web site: Bash Style Guide and Coding Standard. Lug.fh-swf.de.
  3. Web site: Archived copy . 2014-06-23 . dead . https://web.archive.org/web/20140626134128/http://google-styleguide.googlecode.com/svn/trunk/shell.xml . 2014-06-26 .
  4. Web site: Archived copy . 2015-02-15 . dead . https://web.archive.org/web/20150214004842/https://www.kernel.org/doc/Documentation/CodingStyle . 2015-02-14 .
  5. Web site: Linux kernel coding style. 2022-07-21. live. https://web.archive.org/web/20220611181038/https://www.kernel.org/doc/html/latest/process/coding-style.html. 2022-06-11.
  6. Web site: C STYLE GUIDE. August 1994. NASA Technical Reports Server.
  7. Web site: Staley. Clinton. 2006. C Program Style Rules. Cal Poly Department of Computer Science & Software Engineering.
  8. Web site: 2015-11-07. Code Style Guidelines. 2021-12-31. WebKit.
  9. http://google-styleguide.googlecode.com/svn/trunk/cppguide.html#Spaces_vs._Tabs
  10. Web site: C# Coding Conventions (C# Programming Guide). Bill Wagner. Msdn.microsoft.com. 3 September 2017.
  11. Web site: CSS formatting guidelines . Drupal.org . 14 January 2013 . 2014-01-18.
  12. Web site: CSS · Styleguide · GitHub . Github.com . 2014-01-18.
  13. Web site: Google HTML/CSS Style Guide . Indentation . Indent by 2 spaces at a time. . Google GitHub . 2016-03-13 . https://web.archive.org/web/20160313125550/https://google-styleguide.googlecode.com/svn/trunk/htmlcssguide.xml#Indentation . 2016-03-13 . dead . This is an HTML-rendered copy of the official version, which is maintained in XML format
  14. Web site: styleguide: Style guides for Google-originated open-source projects. 3 September 2017. Github.com. 3 September 2017.
  15. Web site: WordPress › CSS Coding Standards « Make WordPress Core . Make.wordpress.org . 17 July 2012 . 2018-07-20.
  16. Web site: Delphi's Object Pascal Style Guide - White Space Usage. 13 December 2022 . en-us.
  17. Web site: F# code formatting guidelines . docs.microsoft.com . 14 October 2020 . en-us.
  18. Web site: Effective Go – The Go Programming Language. golang.org. 3 September 2017.
  19. Web site: HaskellStyleGuide – ganeti – Style Guide for the Haskell code – Cluster-based virtualization management software – Google Project Hosting . Code.google.com . 2014-01-08 . 2014-01-18 . dead . https://web.archive.org/web/20130627194135/http://code.google.com/p/ganeti/wiki/HaskellStyleGuide . June 27, 2013 .
  20. Web site: HTML Tidy Configuration Options Quick Reference . Tidy.sourceforge.net . 2008-06-18 . 2014-01-18.
  21. Web site: Code Conventions for the Java Programming Language: 4. Indentation . Oracle.com . 2014-01-18.
  22. Web site: Code Style Guidelines for Contributors | Android Developers . Source.android.com . 2014-01-18.
  23. Web site: Code Conventions for the JavaScript Programming Language . Javascript.crockford.com . 2006-11-13 . 2014-01-18.
  24. Web site: JavaScript 路 Styleguide . Github.com . 2014-01-18 . dead . https://web.archive.org/web/20130815075924/https://github.com/styleguide/javascript . 2013-08-15 .
  25. Web site: JaveScript Style Rules . Google-styleguide.googlecode.com . 2014-02-18 . https://web.archive.org/web/20140216111413/http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml#Code_formatting . 2014-02-16 . dead .
  26. Web site: jQuery Foundation - jquery.org . JavaScript Style Guide | Contribute to jQuery . Contribute.jquery.org . 2014-01-18.
  27. Web site: Style Guide · The Julia Language . docs.julialang.org.
  28. Web site: Coding Conventions – Kotlin Programming Language . Kotlin.
  29. Web site: lua-users wiki: Lua Style Guide. lua-users.org. 3 September 2017.
  30. Web site: perlstyle: Perl style guide . Perl 5 Version 32.1 Documentation . Jon (JJ) . Allen . Perl Foundation . 2021-04-19.
  31. Web site: Coding standards . Drupal.org . 2014-01-18.
  32. Web site: Manual :: Indenting and Line Length . Pear.php.net . 2014-01-18.
  33. Web site: PHP Coding Standard (draft) – Contributors – Zend Framework Wiki . Framework.zend.com . 2014-01-18 . https://web.archive.org/web/20080103203245/http://framework.zend.com/wiki/display/ZFDEV/PHP+Coding+Standard+(draft) . 2008-01-03 . dead .
  34. Web site: Style Guide : CodeIgniter User Guide . Ellislab.com . 2014-01-18.
  35. Web site: WordPress › PHP Coding Standards « Make WordPress Core . Make.wordpress.org . 23 July 2012 . 2014-01-18.
  36. Web site: PHP : Coding Style Guide . Php-fig.org . 2014-02-18 . dead . https://web.archive.org/web/20140315044508/http://www.php-fig.org/psr/2/ . 2014-03-15 .
  37. Web site: Code Layout and Formatting – PowerShell Practice and Style . PowerShell Practice and Style . 17 October 2021.
  38. Web site: PEP 8 – Style Guide for Python Code. Python.org. 3 September 2017.
  39. Web site: ruby-style-guide: A community-driven Ruby coding style guide. Bozhidar. Batsov. 3 September 2017. 3 September 2017. Github.com.
  40. Web site: rust: A safe, concurrent, practical language. 3 September 2017. Github.com. 3 September 2017.
  41. Web site: Indentation – Scala Documentation. docs.scala-lang.org. 3 September 2017.
  42. Web site: Swift Style Guide . google.github.io . 17 October 2021.
  43. Web site: Tcl Style Guide. Ray Johnson. Tcl.tk. 3 September 2017.
  44. Web site: Structured Coding Conventions. msdn.microsoft.com. 4 January 2007 . 3 September 2017.
  45. Web site: Google XML Document Format Style Guide . google.github.io . Google . 17 October 2021.