Indentation style
In computer programming, indentation style is a convention or style, governing the indentation of lines of source code. An indentation style generally specifies a consistent number of whitespace characters before each line of a block, so that the lines of code appear to be related, and dictates whether to use spaces or tabs as the indentation character. Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.
In computer programming, indentation style is a convention or style, governing the indentation of lines of source code. An indentation style generally specifies a consistent number of whitespace characters before each line of a block, so that the lines of code appear to be related, and dictates whether to use spaces or tabs as the indentation character. int main(int argc, char *argv[]) { while (x == y) { do_something(); do_something_else(); if (some_error) fix_issue(); // single-statement block without braces else continue_as_usual(); } final_thing(); } bool is_negative(int x) { if (x Although not required by languages such as C/C++, using braces for single-statement blocks ensures inserting a new statement does not result in control flow that disagrees with indenting, as seen for example in Apple's infamous goto fail bug. Some sources disagree as to the meaning of One True Brace Style – it might have slight differences based on most prevalent style for given language, individual authors might declare vastly distinct style as OTBS according to their subjective preferences, while others note it as "hacker jargon" for K&R. int power(int x, int y) { int result; if (y 0) result *= x; } return result; } if (x Stroustrup extends K&R style for classes, writing them as follows: class DoubleVec { public: // construct a DoubleVec DoubleVec(int s) :elem(new double[s]), sz(s) { } // element access: subscripting double& operator[](int i) { return elem[i]; } int size() { return sz; } private: // pointer to the elements double* elem; // number of elements int sz; }; Stroustrup does not indent the labels and . Also, in this style, while the opening brace of a function starts on a new line, the opening brace of a class is on the same line as the class name. Stroustrup allows writing short functions all on one line. Stroustrup style is a named indentation style available in the editor Emacs. Stroustrup encourages a K&R-derived style layout with C++ as stated in his modern C++ Core Guidelines. while (x == y) { something(); something_else(); } final_thing(); if (data != NULL && res > 0) { if (JS_DefineProperty(cx, o, "data", STRING_TO_JSVAL(JS_NewStringCopyN(cx, data, res)), NULL, NULL, JSPROP_ENUMERATE) != 0) { QUEUE_EXCEPTION("Internal error!"); goto err; } PQfreemem(data); } else { if (JS_DefineProperty(cx, o, "data", OBJECT_TO_JSVAL(NULL), NULL, NULL, JSPROP_ENUMERATE) != 0) { QUEUE_EXCEPTION("Internal error!"); goto err; } } static JSBool pgresult_constructor(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { QUEUE_EXCEPTION("PGresult class not user-instantiable"); return (JS_FALSE); } while (x == y) { something(); something_else(); } final_thing(); 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 . (* Example Allman code indentation style in Pascal *) procedure dosomething(x, y: Integer); begin while x = y do begin something(); something_else(); end; end; Consequences of this style are that the indented code is clearly set apart from the containing statement by lines that are almost all whitespace and the closing brace lines up in the same column as the opening brace. Some people feel this makes it easy to find matching braces. The blocking style also delineates the block of code from the associated control statement. Commenting out or removing a control statement or block of code, or code refactoring, are all less likely to introduce syntax errors via dangling or missing braces. Also, it is consistent with brace placement for the outer-function block. For example, the following is still correct syntactically: // while (x == y) { something(); something_else(); } As is this: // for (int i=0; i Even like this, with conditional compilation: int c; ifdef HAS_GETCH while ((c = getch()) != EOF) else while ((c = getchar()) != EOF) endif { do_something(c); } while (x == y) { something(); something_else(); } final_thing(); The advantages of this style are similar to those of the Allman style. Blocks are clearly set apart from control statements. The alignment of the braces with the block emphasizes that the full block is conceptually, and programmatically, one compound statement. Indenting the braces emphasizes that they are subordinate to the control statement. The ending brace no longer lines up with the statement, but instead with the opening brace. An example: if (data != NULL && res > 0) { if (!JS_DefineProperty(cx, o, "data", STRING_TO_JSVAL(JS_NewStringCopyN(cx, data, res)), NULL, NULL, JSPROP_ENUMERATE)) { QUEUE_EXCEPTION("Internal error!"); goto err; } PQfreemem(data); } else if (!JS_DefineProperty(cx, o, "data", OBJECT_TO_JSVAL(NULL), NULL, NULL, JSPROP_ENUMERATE)) { QUEUE_EXCEPTION("Internal error!"); goto err; } else if are treated as statement, much like the #elif preprocessor statement. static char * concat (char *s1, char *s2) { while (x == y) { something (); something_else (); } final_thing (); } This style combines the advantages of Allman and Whitesmiths, thereby removing the possible Whitesmiths disadvantage of braces not standing out from the block. One disadvantage is that the ending brace no longer lines up with the statement it conceptually belongs to. Another possible disadvantage is that it might waste space by using two visual levels of indents for one conceptual level, but in reality this is unlikely because, in systems with single-level indentation, each level is usually at least 4 spaces, same as 2 * 2 spaces in GNU style. The GNU Coding Standards recommend this style, and nearly all maintainers of GNU project software use it. The GNU Emacs text editor and the GNU systems' indent command will reformat code according to this style by default. Those who do not use GNU Emacs, or similarly extensible/customisable editors, may find that the automatic indentation settings of their editor are unhelpful for this style. However, many editors defaulting to KNF style cope well with the GNU style when the tab width is set to two spaces; likewise, GNU Emacs adapts well to KNF style by simply setting the tab width to eight spaces. In both cases, automatic reformatting destroys the original spacing, but automatic line indenting will work properly. Steve McConnell, in his book Code Complete, advises against using this style: he marks a code sample which uses it with a "Coding Horror" icon, symbolizing especially dangerous code, and states that it impedes readability. The Linux kernel coding style documentation also recommends against this style, urging readers to burn a copy of the GNU coding standards as a "great symbolic gesture". while (x == y) { something(); something_else(); //... if (x This style combines the advantages of Allman by keeping the vertical alignment of the braces for readability, and identifying blocks easily, with the saving of a line of the K&R style. However, the 2003 edition now uses Allman style throughout. // In C for (i = 0; i // C for (i = 0; i Lisp (dotimes (i 10) (if (= (rem i 2) 0) (do-something i) (progn (do-something-else i) (do-third-thing i)))) Note: progn is a procedure for evaluating multiple sub-expressions sequentially for effects, while discarding all but the final (nth) return value. If all return values are desired, the values procedure would be used. braceless = do text In Haskell, layout can replace braces. Usually the braces and semicolons are omitted for procedural do sections and the program text in general, but the style is commonly used for lists, records and other syntactic elements made up of some pair of parentheses or braces, which are separated with commas or semicolons. If code following the keywords where , let , or of omits braces and semicolons, then indentation is significant. life←{⊃1⍵∨.∧3 4=+/+⌿¯1 0 1∘.⊖¯1 0 1⌽¨⊂⍵} APL style C resembles the terse style of APL code, and is commonly used in their implementations. This style was pioneered by Arthur Whitney, and is heavily used in the implementation of K, Arthur's own project. The J programming language is implemented in this style as well. Notably, not all implementations of APL use this style of C, namely: GNU APL and Dyalog APL. In addition to APL style C indentation, typically the names are shortened to either single or double characters: To reduce the amount of indentation, and expressions spanning multiple lines. for (int i = 0; i if (x A disadvantage is maintaining the same code in multiple locations above and below the block. Some editors provide support for maintaining block awareness. A folding editor can hide (fold) and reveal (unfold) blocks by indentation level. Some editors highlight matching braces when the cursor is positioned next to one. ==See also== Secondary notation Syntax highlighting Bachmanity Insanity, a Silicon Valley episode featuring the topic Source-code formatter ==References== ==External links== C Style: Standards and Guidelines: Defining Programming Standards for Professional C Programmers, Prentice Hall, / (full text is also online). Straker, David (1992). Contextual Indent GNU Coding Standards ===Tabs and spaces=== Tabs versus Spaces: An Eternal Holy War by Jamie Zawinski Why I prefer no tabs in source code by Adam Spiers Why I love having tabs in source code (archived) Elastic tabstops – the solution to the tabs-versus-spaces issue
References
- A Methodology For Studying The Psychological Complexity of Computer Programs . · 1787871600.0
- ESLint brace-style . retrieved 24 February 2026 · 1787871600.0
- The One True Brace Style - Ailanto . 6 April 1998. retrieved 4 August 2025 · 1787871600.0
- The Jargon File . 29 December 2003. retrieved 18 August 2014 · 1787871600.0
- A detailed description of the style is given at [https://www.kernel.org/doc/html/latest/process/coding-style.html kernel.org] . · 1787871600.0
- Larabel, Michael. The Linux Kernel Deprecates The 80 Character Line Coding Style . · 1787871600.0
- Reddy, Achut. Java Coding Style Guide . Sun Microsystems . 30 March 200. · 1787871600.0
- Java Code Conventions . Sun Microsystems . 12 September 1997. retrieved 30 May 2008 · 1787871600.0
- Code Conventions for the Java Programming Language . Sun Microsystems . 20 March 1997. · 1787871600.0
- Stroustrup, Bjarne. PPP Style Guide . September 2010. · 1787871600.0
- if (x Stroustrup extends K&R style for classes, writing them as follows: class DoubleVec { public: // construct a DoubleVec DoubleVec(int s) :elem(new double[s]), sz(s) { } // element access: subscrip . · 1787871600.0
- Indentation in Source Code: A Randomized Control Trial on the Reada . · 1787871600.0
- Shannon, Bill. C Style and Coding Standards for SunOS . retrieved 15 June 2019 · 1787871600.0
- DTraceToolkit Style Guide . · 1787871600.0
- Shannon, Bill. cstyle.pl . illumos-gate . 9 September 1998. · 1787871600.0
- while (x == y) { something(); something_else(); } final_thing(); This style is similar to the standard indentation used by the Pascal (programming language)|Pascal languages and Transact-SQL . · 1787871600.0
- while (x == y) { something(); something_else(); } final_thing(); The advantages of this style are similar to those of the #Allman style|Allman style. Blocks are clearly set apart from control stat . · 1787871600.0
- Stall, Richard. My Lisp Experiences and the Development of GNU Emacs (Transcript of speech at the International Lisp Conference) . · 1787871600.0
- Baumann, Richard. archive.org . · 1787871600.0
- W. M. McKeeman, J. J. Horning, and D. B. Wortman, A Compiler Generator, 1970, https://archive.org/details/compilergenerato00mcke . · 1787871600.0
- McConnell, Steve. Code Complete: A practical handbook of software construction . M . · 1787871600.0
- Linux kernel coding style . retrieved 1 January 2017 · 1787871600.0
- Cite book |last1=Hanenberg |first1=Stefan |last2=Morzeck |first2=Johannes |last3=Werger |first3=Ole |last4=Gries |first4=Stefan |last5=Gruhn |first5=Volker |chapter=Indentation and Reading Time: A C . · 1787871600.0
- [https://www.horstmann.com/bigcpp/styleguide.html Horstmann Style Guide] . · 1787871600.0
- Lammers, Susan. Programmers at Work . Microsoft Press . 1986. · 1787871600.0
- Pattee, Jim. Artistic Style 2.05 Documentation . Artistic Style . retrieved 24 April 2015 · 1787871600.0
- The style can make visual scanning easier for some, since the headers of any block are the only thing exdented at that level (the theory being that the closing control of the prior block interfere . · 1787871600.0
- The Haskell 98 Report . haskell.org . retrieved 3 March 2016 · 1787871600.0
- Lipovača. Making Our Own Types and Typeclasses . learnyouahaskell.com . retrieved 3 February 2016 · 1787871600.0
- The J Incunabulum . jsoftware.com . retrieved 19 May 2022 · 1787871600.0
- Java Style Guide . · 1787871600.0
- Egyptian brackets . Foldoc . · 1787871600.0
- Google JavaScript Style Guide . · 1787871600.0
- 1TBS . · 1787871600.0
- Artistic Style . retrieved 4 August 2025 · 1787871600.0
- Brace styles and JavaScript . 7 January 2013. retrieved 8 November 2018 · 1787871600.0
Cite this
"Indentation style". 1787946955. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1371851926 JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy