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
Pretty-printing
(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!
==Programming code formatting== Programmers often use tools to format [[programming language]] [[source code]] in a particular manner. Proper code formatting makes it easier to read and understand. Different programmers often prefer different styles of formatting, such as the use of code [[indentation (typesetting)|indentation]] and whitespace or positioning of [[bracket|braces]]. A code formatter or [[code indenter]] converts source code from one format style to another. This is relatively straightforward because of the unambiguous syntax of programming languages. Code beautification involves parsing the source code into component structures, such as assignment statements, ''if'' blocks, [[program loop|loop]]s, etc. (see also [[control flow]]), and formatting them in a manner specified by the user in a configuration file. Code beautifiers exist as standalone applications and built into [[text editor]]s and [[integrated development environment]]s. For example, [[Emacs]]' various language modes can correctly [[indentation (typesetting)|indent]] blocks of code attractively.<ref>{{cite web |url=https://www.gnu.org/software/emacs/manual/html_node/emacs/Program-Indent.html#Program-Indent |title=Indentation for Programs |first=Richard M. |last=Stallman |publisher=Free Software Foundation |work=GNU Emacs Manual |accessdate=20 October 2011}}</ref> ===HTML=== {{see|HTML Tidy}} ===Lisp pretty-printer=== An early example of pretty-printing was [[Bill Gosper]]'s "GRINDEF" (''i.e.'' 'grind function') program (''c.'' 1967), which used [[combinatorial optimization|combinatorial search]] with pruning to format [[Lisp programming language|LISP]] programs. Early versions operated on the executable (list structure) form of the Lisp program and were oblivious to the special meanings of various functions. Later versions had special read conventions for incorporating non-executable comments and also for preserving [[read macro]]s in unexpanded form. They also allowed special indentation conventions for special functions such as <code>if</code>.<ref>Ira Goldstein, "Pretty Printing : Converting List to Linear Structure", Artificial Intelligence Memo 279, Massachusetts Institute of Technology, February 1973. [http://www.softwarepreservation.org/projects/LISP/MIT/AIM-279-Goldstein-Pretty_Printing.pdf/view full text]</ref><ref>Richard C. Waters, "Using the new common Lisp pretty printer", ''ACM SIGPLAN Lisp Pointers'' '''5''':2:27-34, April–June 1992. [https://web.archive.org/web/20170706120420/ftp://publications.ai.mit.edu/ai-publications/pdf/AIM-1102.pdf full text]</ref> The term "grind" was used in some Lisp circles as a synonym for pretty-printing.<ref>[[Jargon File]], ''s.v.'' grind</ref> ===Project style rules=== Many open source projects have established rules for code layout. The most typical are the [[GNU]] formatting<ref>[https://www.gnu.org/prep/standards/standards.html#Formatting GNU style]</ref> and the BSD style.<ref>[http://www.freebsd.org/cgi/man.cgi?query=style&sektion=9 BSD style]</ref> The biggest difference between the two is the location of the braces: in the GNU style, opening and closing braces are on lines by themselves, with the same indent. BSD style places an opening brace at the end of the preceding line, and the closing braces can be followed by '''else'''. The size of indent and location of whitespace also differs. ===Example of formatting and beautifying code=== The following example shows some typical C structures and how various [[indentation style]] rules format them. Without any formatting at all, it looks like this: <syntaxhighlight lang="c"> int foo(int k){if(k<1||k>2){printf("out of range\n"); printf("this function requires a value of 1 or 2\n");}else{ printf("Switching\n");switch(k){case 1:printf("1\n");break;case 2:printf("2\n");break;}}} </syntaxhighlight> The [[indent (Unix)|GNU indent program]] produces the following output when asked to indent according to the [[indent style#GNU style|GNU rules]]: <syntaxhighlight lang="c"> int foo (int k) { if (k < 1 || k > 2) { printf ("out of range\n"); printf ("this function requires a value of 1 or 2\n"); } else { printf ("Switching\n"); switch (k) { case 1: printf ("1\n"); break; case 2: printf ("2\n"); break; } } } </syntaxhighlight> It produces this output when formatting according to BSD rules: <syntaxhighlight lang="c"> int foo(int k) { if (k < 1 || k > 2) { printf("out of range\n"); printf("this function requires a value of 1 or 2\n"); } else { printf("Switching\n"); switch (k) { case 1: printf("1\n"); break; case 2: printf("2\n"); break; } } } </syntaxhighlight>
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)