Indentation (typesetting)
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.
Indentation 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.
- For first-line indentation the first line of a paragraph is indented.
- A first-line indentation of 2 em:Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
- For hanging indentation all but the first line of a paragraph is indented.
- A hanging indentation of 2 em:Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
- For block indentation the entire paragraph is indented relative to the preceding paragraph.
- A block indentation of 2 em on the left:Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
- Block indentation on both sides is commonly used for a quotation, a block quotation, here shown with 2 em on the left and right but since it is not right-aligned, the empty space on the right varies based on line length:Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
- Here's the same as the previous but justified (left and right aligned):Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Indentation in programming
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 |
---|---|---|
Bash | Yes | Fritz Mehner's style guide suggests 2, 4, or 8 spaces and uses 2 in all examples[2] |
C | Yes | The Linux kernel uses 1 tab[4][5]
Clinton Staley advocates 3 spaces[7] |
C++ | Yes | WebKit recommends 4 spaces[8]
Google uses 2 spaces[9] |
C# | Yes | Microsoft convention: 4 spaces[10] |
CSS | Yes | Drupal, GitHub, and Google use 2 spaces[11][12][13][14] |
Dart | Yes | dart format uses 2 spaces
|
Delphi | Yes | Delphi style guide: 2 spaces [16] |
F# | Yes | F# style guide: 4 spaces[17] |
Go | Yes | Effective Go: 1 tab [18] |
Haskell | No | Google uses 2 spaces[19] |
HTML | Yes | Google uses 2 spaces[13] |
Java | Yes | Oracle uses 4 spaces[21]
Most Eclipse IDE components use tabs |
JavaScript | Yes | Douglas Crockford advocates 4 spaces[23]
GitHub and Google use 2 spaces[24][25] Firefox's built-in jsbeautifier defaults to 2 spaces prettyprinter in Google Chrome and Internet Explorer use 4 spaces |
Julia | Yes | Julia style guide: 4 spaces[27] |
Kotlin | Yes | Kotlin style guide: 4 spaces[28] |
Lua | Yes | Lua style guide: 2 spaces[29] |
Perl | Yes | Larry Wall prefers 4 spaces[30] |
PHP | Yes | 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] |
PowerShell | Yes | Unofficial PowerShell Best Practices and Style Guide: 4 spaces[37] |
Python | No | PEP 8: 4 spaces[38] |
Ruby | Yes | Ruby style guide: 2 spaces [39] |
Rust | Yes | Rust style guide: 4 spaces [40] |
Scala | Yes | Scala style guide: 2 spaces [41] |
Swift | Yes | Google uses 2 spaces[42] |
Tcl | Yes | Tcl style guide: 4 spaces [43] |
Visual Basic | Yes | Microsoft convention: 4 spaces [44] |
XML | Yes | Google uses 2 spaces[45] |
Elastic tabstops
In 2006, a new method of indentation was proposed, called elastic tabstops.[citation needed]
References
- ^ "Tabs versus Spaces: An Eternal Holy War". Jwz.org. 5 January 2007. Retrieved 18 January 2014.
- ^ "Bash Style Guide and Coding Standard" (PDF). Lug.fh-swf.de.
- ^ "Archived copy". Archived from the original on 26 June 2014. Retrieved 23 June 2014.
{{cite web}}
: CS1 maint: archived copy as title (link) - ^ "Archived copy". Archived from the original on 14 February 2015. Retrieved 15 February 2015.
{{cite web}}
: CS1 maint: archived copy as title (link) - ^ "Linux kernel coding style". Archived from the original on 11 June 2022. Retrieved 21 July 2022.
- ^ "C STYLE GUIDE". NASA Technical Reports Server. August 1994.
- ^ Staley, Clinton (2006). "C Program Style Rules". Cal Poly Department of Computer Science & Software Engineering.
- ^ "Code Style Guidelines". WebKit. 7 November 2015. Retrieved 31 December 2021.
- ^ [1] Archived 2014-10-06 at the Wayback Machine
- ^ Bill Wagner. "C# Coding Conventions (C# Programming Guide)". Msdn.microsoft.com. Retrieved 3 September 2017.
- ^ "CSS formatting guidelines". Drupal.org. 14 January 2013. Retrieved 18 January 2014.
- ^ "CSS · Styleguide · GitHub". Github.com. Retrieved 18 January 2014.
- ^ a b "Google HTML/CSS Style Guide". Google GitHub. Indentation. Archived from the original on 13 March 2016. Retrieved 13 March 2016.
Indent by 2 spaces at a time.
This is an HTML-rendered copy of the official version, which is maintained in XML format - ^ "styleguide: Style guides for Google-originated open-source projects". Github.com. 3 September 2017. Retrieved 3 September 2017.
- ^ "WordPress › CSS Coding Standards « Make WordPress Core". Make.wordpress.org. 17 July 2012. Retrieved 20 July 2018.
- ^ "Delphi's Object Pascal Style Guide - White Space Usage". Retrieved 13 December 2022.
- ^ "F# code formatting guidelines". docs.microsoft.com. Retrieved 14 October 2020.
- ^ "Effective Go – The Go Programming Language". golang.org. Retrieved 3 September 2017.
- ^ "HaskellStyleGuide – ganeti – Style Guide for the Haskell code – Cluster-based virtualization management software – Google Project Hosting". Code.google.com. 8 January 2014. Archived from the original on 27 June 2013. Retrieved 18 January 2014.
- ^ "HTML Tidy Configuration Options Quick Reference". Tidy.sourceforge.net. 18 June 2008. Retrieved 18 January 2014.
- ^ "Code Conventions for the Java Programming Language: 4. Indentation". Oracle.com. Retrieved 18 January 2014.
- ^ "Code Style Guidelines for Contributors | Android Developers". Source.android.com. Retrieved 18 January 2014.
- ^ "Code Conventions for the JavaScript Programming Language". Javascript.crockford.com. 13 November 2006. Retrieved 18 January 2014.
- ^ "JavaScript 路 Styleguide". Github.com. Archived from the original on 15 August 2013. Retrieved 18 January 2014.
- ^ "JaveScript Style Rules". Google-styleguide.googlecode.com. Archived from the original on 16 February 2014. Retrieved 18 February 2014.
- ^ jQuery Foundation - jquery.org. "JavaScript Style Guide | Contribute to jQuery". Contribute.jquery.org. Retrieved 18 January 2014.
- ^ "Style Guide · The Julia Language". docs.julialang.org.
- ^ "Coding Conventions – Kotlin Programming Language". Kotlin.
- ^ "lua-users wiki: Lua Style Guide". lua-users.org. Retrieved 3 September 2017.
- ^ Allen, Jon (JJ) (ed.). "perlstyle: Perl style guide". Perl 5 Version 32.1 Documentation. Perl Foundation. Retrieved 19 April 2021.
- ^ "Coding standards". Drupal.org. Retrieved 18 January 2014.
- ^ "Manual :: Indenting and Line Length". Pear.php.net. Retrieved 18 January 2014.
- ^ "PHP Coding Standard (draft) – Contributors – Zend Framework Wiki". Framework.zend.com. Archived from the original on 3 January 2008. Retrieved 18 January 2014.
- ^ "Style Guide : CodeIgniter User Guide". Ellislab.com. Archived from the original on 1 June 2013. Retrieved 18 January 2014.
- ^ "WordPress › PHP Coding Standards « Make WordPress Core". Make.wordpress.org. 23 July 2012. Retrieved 18 January 2014.
- ^ "PHP : Coding Style Guide". Php-fig.org. Archived from the original on 15 March 2014. Retrieved 18 February 2014.
- ^ "Code Layout and Formatting – PowerShell Practice and Style". PowerShell Practice and Style. Retrieved 17 October 2021.
- ^ "PEP 8 – Style Guide for Python Code". Python.org. Retrieved 3 September 2017.
- ^ Batsov, Bozhidar (3 September 2017). "ruby-style-guide: A community-driven Ruby coding style guide". Github.com. Retrieved 3 September 2017.
- ^ "rust: A safe, concurrent, practical language". Github.com. 3 September 2017. Retrieved 3 September 2017.
- ^ "Indentation – Scala Documentation". docs.scala-lang.org. Retrieved 3 September 2017.
- ^ "Swift Style Guide". google.github.io. Retrieved 17 October 2021.
- ^ Ray Johnson. "Tcl Style Guide" (PDF). Tcl.tk. Retrieved 3 September 2017.
- ^ "Structured Coding Conventions". msdn.microsoft.com. 4 January 2007. Retrieved 3 September 2017.
- ^ "Google XML Document Format Style Guide". google.github.io. Google. Retrieved 17 October 2021.