Indentation (typesetting)

Last updated

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:

Contents

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.

LanguageFree-form?Note
Bash YesFritz Mehner's style guide suggests 2, 4, or 8 spaces and uses 2 in all examples [2]

Google uses 2 spaces [3]

C YesThe Linux kernel uses 1 tab [4] [5]

NASA uses 4 spaces. [6]

Clinton Staley advocates 3 spaces [7]

C++ YesWebKit 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]

WordPress uses tabs [15]

Dart Yesdart format uses 2 spaces
Delphi YesDelphi style guide: 2 spaces [16]
F# YesF# style guide: 4 spaces [17]
Go YesEffective Go: 1 tab [18]
Haskell NoGoogle uses 2 spaces [19]
HTML YesGoogle uses 2 spaces [13]

HTML Tidy defaults to 2 spaces [20]

Java Yes Oracle uses 4 spaces [21]

Android uses 4 spaces [22]

Most Eclipse IDE components use tabs

JavaScript Yes 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

Julia YesJulia style guide: 4 spaces [27]
Kotlin YesKotlin style guide: 4 spaces [28]
Lua YesLua 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 YesUnofficial PowerShell Best Practices and Style Guide: 4 spaces [37]
Python NoPEP 8: 4 spaces [38]
Ruby YesRuby style guide: 2 spaces [39]
Rust YesRust style guide: 4 spaces [40]
Scala YesScala style guide: 2 spaces [41]
Swift YesGoogle uses 2 spaces [42]
Tcl YesTcl style guide: 4 spaces [43]
Visual Basic YesMicrosoft convention: 4 spaces [44]
XML YesGoogle uses 2 spaces [45]

Elastic tabstops

In 2006, a new method of indentation was proposed, called elastic tabstops.[ citation needed ]

Related Research Articles

<span class="mw-page-title-main">Foobar</span> Placeholder names in programming

The terms foobar, foo, bar, baz, qux, quux, and others are used as metasyntactic variables and placeholder names in computer programming or computer-related documentation. They have been used to name entities such as variables, functions, and commands whose exact identity is unimportant and serve only to demonstrate a concept. The style guide for Google developer documentation recommends against using them as example project names because they are unclear and can cause confusion.

<span class="mw-page-title-main">Python (programming language)</span> General-purpose programming language

Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation.

<span title="Latin-language text"><i lang="la">Scriptio continua</i></span> Style of writing without spaces between words

Scriptio continua, also known as scriptura continua or scripta continua, is a style of writing without spaces or other marks between the words or sentences. The form also lacks punctuation, diacritics, or distinguished letter case. In the West, the oldest Greek and Latin inscriptions used word dividers to separate words in sentences; however, Classical Greek and late Classical Latin both employed scriptio continua as the norm.

<i>Lorem ipsum</i> Placeholder text used in publishing and graphic design

In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before the final copy is available. It is also used to temporarily replace text in a process called greeking, which allows designers to consider the form of a webpage or publication, without the meaning of the text influencing the design.

<span class="mw-page-title-main">Syntax highlighting</span> Tool of editors for programming, scripting, and markup

Syntax highlighting is a feature of text editors that is used for programming, scripting, or markup languages, such as HTML. The feature displays text, especially source code, in different colours and fonts according to the category of terms. This feature facilitates writing in a structured language such as a programming language or a markup language as both structures and syntax errors are visually distinct. This feature is also employed in many programming related contexts, either in the form of colorful books or online websites to make understanding code snippets easier for readers. Highlighting does not affect the meaning of the text itself; it is intended only for human readers.

In computer programming, indentation style is a convention, a.k.a. style, governing the indentation of blocks of source code. An indentation style generally involves consistent width of whitespace before each line of a block, so that the lines of code appear to be related, and dictates whether to use space or tab characters for the indentation whitespace.

Pretty-printing is the application of any of various stylistic formatting conventions to text files, such as source code, markup, and similar kinds of content. These formatting conventions may entail adhering to an indentation style, using different color and typeface to highlight syntactic elements of source code, or adjusting size, to make the content easier for people to read, and understand. Pretty-printers for source code are sometimes called code formatters or beautifiers.

<span class="mw-page-title-main">Code folding</span> Tool of editors for programming, scripting and markup

Code or text folding, or less commonly holophrasting, is a feature of some graphical user interfaces that allows the user to selectively hide ("fold") or display ("unfold") parts of a document. This allows the user to manage large amounts of text while viewing only those subsections that are currently of interest. It is typically used with documents which have a natural tree structure consisting of nested elements. Other names for these features include expand and collapse, code hiding, and outlining. In Microsoft Word, the feature is called "collapsible outlining".

TextPad is a text editor for Microsoft Windows developed by Helios Software Solutions. It is currently in its ninth major version. TextPad was initially released in 1992 as shareware, with users requested to pay a registration fee to support future development. As of 1996 the company was an associate member of the Association of Shareware Professionals. By 1998 the company was pointing out that the editor was "shareware " and payment was necessary to continue to use it.

A lightweight markup language (LML), also termed a simple or humane markup language, is a markup language with simple, unobtrusive syntax. It is designed to be easy to write using any generic text editor and easy to read in its raw form. Lightweight markup languages are used in applications where it may be necessary to read the raw document as well as the final rendered output.

In word processing and digital typesetting, a non-breaking space, also called NBSP, required space, hard space, or fixed space, is a space character that prevents an automatic line break at its position. In some formats, including HTML, it also prevents consecutive whitespace characters from collapsing into a single space. Non-breaking space characters with other widths also exist.

Line breaking, also known as word wrapping, is breaking a section of text into lines so that it will fit into the available width of a page, window or other display area. In text display, line wrap is continuing on a new line when a line is full, so that each line fits into the viewable window, allowing text to be read from top to bottom without any horizontal scrolling. Word wrap is the additional feature of most text editors, word processors, and web browsers, of breaking lines between words rather than within words, where possible. Word wrap makes it unnecessary to hard-code newline delimiters within paragraphs, and allows the display of text to adapt flexibly and dynamically to displays of varying sizes.

<span class="mw-page-title-main">Initial</span> Oversized first letter in a text block

In a written or published work, an initial is a letter at the beginning of a word, a chapter, or a paragraph that is larger than the rest of the text. The word is ultimately derived from the Latin initiālis, which means of the beginning. An initial is often several lines in height, and, in older books or manuscripts, may take the form of an inhabited or historiated initial. Certain important initials, such as the Beatus initial, or B, of Beatus vir... at the opening of Psalm 1 at the start of a vulgate Latin. These specific initials in an illuminated manuscript were also called initia.

Secondary notation is the set of visual cues used to improve the readability of a formal notation. Examples of secondary notation include the syntax highlighting of computer source code, sizes and color codes for easy recognition of consumer symbols such as bank notes or coins, or the regular typographic conventions often found in technical books to highlight sections with the same type of content.

The fmt command in Unix, Plan 9, Inferno, and Unix-like operating systems is used to format natural language text for humans to read.

The zero-width space (ZWSP) is a non-printing character used in computerized typesetting to indicate where the word boundaries are, without actually displaying a visible space in the rendered text. This enables text-processing systems for scripts that do not use explicit spacing to recognize where word boundaries are for the purpose of handling line breaks appropriately. Zero-width space is unicode character U+200B, and is located in the unicode General Punctuation block, and can be represented by numeric character references &#x200B; or &#8203;.

<span class="mw-page-title-main">Sass (style sheet language)</span> Stylesheet language

Sass is a preprocessor scripting language that is interpreted or compiled into Cascading Style Sheets (CSS). SassScript is the scripting language itself.

fold is a Unix command used for making a file with long lines more readable on a limited width computer terminal by performing a line wrap.

CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell in an effort to enhance JavaScript's brevity and readability. Specific additional features include list comprehension and destructuring assignment.

References

  1. "Tabs versus Spaces: An Eternal Holy War". Jwz.org. 5 January 2007. Retrieved 18 January 2014.
  2. "Bash Style Guide and Coding Standard" (PDF). Lug.fh-swf.de.
  3. "Archived copy". Archived from the original on 26 June 2014. Retrieved 23 June 2014.{{cite web}}: CS1 maint: archived copy as title (link)
  4. "Archived copy". Archived from the original on 14 February 2015. Retrieved 15 February 2015.{{cite web}}: CS1 maint: archived copy as title (link)
  5. "Linux kernel coding style". Archived from the original on 11 June 2022. Retrieved 21 July 2022.
  6. "C STYLE GUIDE". NASA Technical Reports Server. August 1994.
  7. Staley, Clinton (2006). "C Program Style Rules". Cal Poly Department of Computer Science & Software Engineering.
  8. "Code Style Guidelines". WebKit. 7 November 2015. Retrieved 31 December 2021.
  9. Archived 2014-10-06 at the Wayback Machine
  10. Bill Wagner. "C# Coding Conventions (C# Programming Guide)". Msdn.microsoft.com. Retrieved 3 September 2017.
  11. "CSS formatting guidelines". Drupal.org. 14 January 2013. Retrieved 18 January 2014.
  12. "CSS · Styleguide · GitHub". Github.com. Retrieved 18 January 2014.
  13. 1 2 "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
  14. "styleguide: Style guides for Google-originated open-source projects". Github.com. 3 September 2017. Retrieved 3 September 2017.
  15. "WordPress › CSS Coding Standards « Make WordPress Core". Make.wordpress.org. 17 July 2012. Retrieved 20 July 2018.
  16. "Delphi's Object Pascal Style Guide - White Space Usage" . Retrieved 13 December 2022.
  17. "F# code formatting guidelines". docs.microsoft.com. Retrieved 14 October 2020.
  18. "Effective Go – The Go Programming Language". golang.org. Retrieved 3 September 2017.
  19. "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.
  20. "HTML Tidy Configuration Options Quick Reference". Tidy.sourceforge.net. 18 June 2008. Retrieved 18 January 2014.
  21. "Code Conventions for the Java Programming Language: 4. Indentation". Oracle.com. Retrieved 18 January 2014.
  22. "Code Style Guidelines for Contributors | Android Developers". Source.android.com. Retrieved 18 January 2014.
  23. "Code Conventions for the JavaScript Programming Language". Javascript.crockford.com. 13 November 2006. Retrieved 18 January 2014.
  24. "JavaScript 路 Styleguide". Github.com. Archived from the original on 15 August 2013. Retrieved 18 January 2014.
  25. "JaveScript Style Rules". Google-styleguide.googlecode.com. Archived from the original on 16 February 2014. Retrieved 18 February 2014.
  26. jQuery Foundation - jquery.org. "JavaScript Style Guide | Contribute to jQuery". Contribute.jquery.org. Retrieved 18 January 2014.
  27. "Style Guide · The Julia Language". docs.julialang.org.
  28. "Coding Conventions – Kotlin Programming Language". Kotlin.
  29. "lua-users wiki: Lua Style Guide". lua-users.org. Retrieved 3 September 2017.
  30. Allen, Jon (JJ) (ed.). "perlstyle: Perl style guide". Perl 5 Version 32.1 Documentation. Perl Foundation. Retrieved 19 April 2021.
  31. "Coding standards". Drupal.org. Retrieved 18 January 2014.
  32. "Manual :: Indenting and Line Length". Pear.php.net. Retrieved 18 January 2014.
  33. "PHP Coding Standard (draft) – Contributors – Zend Framework Wiki". Framework.zend.com. Archived from the original on 3 January 2008. Retrieved 18 January 2014.
  34. "Style Guide : CodeIgniter User Guide". Ellislab.com. Archived from the original on 1 June 2013. Retrieved 18 January 2014.
  35. "WordPress › PHP Coding Standards « Make WordPress Core". Make.wordpress.org. 23 July 2012. Retrieved 18 January 2014.
  36. "PHP : Coding Style Guide". Php-fig.org. Archived from the original on 15 March 2014. Retrieved 18 February 2014.
  37. "Code Layout and Formatting – PowerShell Practice and Style". PowerShell Practice and Style. Retrieved 17 October 2021.
  38. "PEP 8 – Style Guide for Python Code". Python.org. Retrieved 3 September 2017.
  39. Batsov, Bozhidar (3 September 2017). "ruby-style-guide: A community-driven Ruby coding style guide". Github.com. Retrieved 3 September 2017.
  40. "rust: A safe, concurrent, practical language". Github.com. 3 September 2017. Retrieved 3 September 2017.
  41. "Indentation – Scala Documentation". docs.scala-lang.org. Retrieved 3 September 2017.
  42. "Swift Style Guide". google.github.io. Retrieved 17 October 2021.
  43. Ray Johnson. "Tcl Style Guide" (PDF). Tcl.tk. Retrieved 3 September 2017.
  44. "Structured Coding Conventions". msdn.microsoft.com. 4 January 2007. Retrieved 3 September 2017.
  45. "Google XML Document Format Style Guide". google.github.io. Google. Retrieved 17 October 2021.