Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Indentation style
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== GNU === {{Main article|GNU coding standards}} Like the [[#Allman style|Allman]] and [[#Whitesmiths|Whitesmiths]] styles, [[GNU]] style puts braces on a line by themselves, indented by two spaces, except when opening a function definition, where they are not indented.<ref name="gnu.org">{{cite web |url=https://www.gnu.org/prep/standards/html_node/Formatting.html |title=Formatting Your Source Code |website=[[GNU Coding Standards]] |access-date=6 June 2016 }}</ref> In either case, the contained code is indented by two spaces from the braces. Popularised by [[Richard Stallman]], the layout may be influenced by his background of writing [[Lisp (programming language)|Lisp]] code.<ref name="gnu.org-lisp">{{cite web |url=https://www.gnu.org/gnu/rms-lisp.html |title=My Lisp Experiences and the Development of GNU Emacs (Transcript of speech at the International Lisp Conference) |first=Richard |last=Stallman |date=28 October 2002 |access-date=6 June 2016}}</ref> In Lisp, the equivalent to a block (a progn) is a first-class data entity, and giving it its own indentation level helps to emphasize that, whereas in C, a block is only syntax. This style can also be found in some [[ALGOL]] and [[XPL]] programming language textbooks from the 1960s and 1970s.<ref>{{cite book |last1=Baumann |first1=Richard |author-link1=:de:Richard Baumann (Mathematiker) |last2=Feliciano |first2=Manuel<!--, Jr.? --> |last3=Bauer |first3=Friedrich Ludwig |author-link3=Friedrich Ludwig Bauer |last4=Samelson |first4=Klaus |author-link4=Klaus Samelson |date=1964 |title=Introduction to ALGOL β A primer for the non-specialist, emphasizing the practical uses of the algorithmic language |series=Series in Automatic Computation |publisher=[[Prentice-Hall, Inc.]] |publication-place=Englewood Cliffs, New Jersey, USA |isbn=0-13-477828-6 |lccn=64-10740 |id=ark:/13960/t6qz35p37 |url=https://archive.org/details/introductiontoal00baum |access-date=2022-10-23}}</ref><ref>W. M. McKeeman, J. J. Horning, and D. B. Wortman, ''A Compiler Generator'', 1970, https://archive.org/details/compilergenerato00mcke</ref>{{discuss|editorial option in lieu of source}} Although not indentation per se, GNU coding style also includes a space after a function name {{endash}} before the left parenthesis of an argument list.<ref name="gnu.org"/> <syntaxhighlight lang=c> static char * concat (char *s1, char *s2) { while (x == y) { something (); something_else (); } final_thing (); } </syntaxhighlight> This style combines the advantages of [[#Allman style|Allman]] and [[#Whitesmiths|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.{{Citation needed|date=February 2013}} The [[GNU Emacs]] text editor and the GNU systems' [[indent (Unix)|indent]] command will reformat code according to this style by default.<ref>Tested on the sample source code above on Ubuntu 18.04 with GNU indent 2.2.11 and GNU Emacs 25.2.2 started with <code>emacs --no-init-file</code>.</ref> 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.<ref name="mcconnell" >{{cite book |title=Code Complete: A practical handbook of software construction |url=https://archive.org/details/codecomplete0000mcco |url-access=registration |last=McConnell |first=Steve |publisher=Microsoft Press |year=2004 |isbn=978-0-7356-1967-8 |location=Redmond, WA |pages=[https://archive.org/details/codecomplete0000mcco/page/746 746β747] |author-link=Steve McConnell}}</ref> 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".<ref name="kernel.coding-style" >{{cite web |title=Linux kernel coding style |url=https://www.kernel.org/doc/Documentation/process/coding-style.rst |access-date=1 January 2017}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)