Rule of 72: Difference between revisions
Line 1: | Line 1: | ||
{{one source|date=June 2011}} | |||
'''Prettyprint''' (or '''pretty-print''') is the application of any of various stylistic [[Text formatting|formatting]] conventions to [[Text-based (computing)|text]], [[source code]], [[Markup language|markup]], and other similar kinds of content. These formatting conventions usually consist of changes in positioning, spacing, color, contrast, size and similar modifications intended to make the content easier for people to view, read, and understand. Prettyprinters for programming language source code are sometimes called code beautifiers or [[Syntax highlighting|syntax highlighters]]. | |||
==Pretty-printing mathematics== | |||
[[Image:MathematicaTypesetExpression.png|thumb|210px|A typeset mathematical expression]] | |||
Pretty-printing usually refers to displaying [[mathematical expression]]s in a way that is similar to the way they are [[formula editor|typeset]] professionally. For example, in [[computer algebra system]]s such as [[Maxima (software)|Maxima]] or [[Mathematica]] the system may write output like "<tt>x ^ 2 + 3 * x</tt>" as "<math>x^2+3x</math>". Some [[graphing calculator]]s, such as the [[Casio 9860 series]], [[HP-49 series]], [[TI-89]], and [[TI-Nspire]], or the [[TI-83 Plus]]/[[TI-84 Plus]] with the [http://www.ticalc.org/archives/files/fileinfo/208/20807.html PrettyPt] add-on, can perform pretty-printing. Additionally, a number of newer scientific calculators are equipped with dot matrix screens capable of pretty-printing such as the Casio FX-ES series (Natural Display), Sharp EL-W series (WriteView), HP SmartCalc 300s, and [[TI-30#TI-30_models|TI-30XB]]. | |||
Many text formatting programs can also typeset mathematics: [[TeX]] was developed specifically for high-quality mathematical typesetting. | |||
==Pretty-printing markup and tag based code== | |||
Pretty-printing in markup language instances is most typically associated with [[Indentation#Indentation_in_typesetting|indentation]] of tags and string content to visually determine hierarchy and nesting. Although the syntactical structures of tag based languages do not significantly vary, indentation may vary significantly due to how a markup language is interpreted or in reflect to the data they describe. | |||
In [[MathML]] white space characters do not reflect data, meaning, or syntax above what is required by [[XML]] syntax. In [[HTML]] white space characters between tags are considered text and are parsed as text nodes into the parsed result.<ref>{{cite web |url=https://developer.mozilla.org/en-US/docs/Whitespace_in_the_DOM |title=Whitespace in the DOM |first=L. David |last=Baron |publisher=Mozilla Developer Network |accessdate=27 August 2012}}</ref> While indentation may be generously applied to a MathML document sufficient additional care must be taken in the pretty print operation of a HTML document to ensure additional text nodes are not created or destroyed in general proximity to content or content reflective tag elements. This difference in complexity is non-trivial from the perspective of an automated pretty-print operation where no special rules or edge cases are necessary in the more simple MathML example. The HTML example may require a series of progressive interrelated algorithms to account for various patterns of tag elements and content that conforms to a uniform style and is consistent in application across various instances as evidenced by the [https://github.com/austincheney/Pretty-Diff/blob/master/lib/markup_beauty.js markup_beauty.js] application component used to beautify HTML, XML, and related technologies for the [[Pretty Diff]] tool. | |||
==Programming code formatting and beautification== | |||
Programmers often use tools to format [[programming language]] [[source code]] in a particular manner. Proper code formatting makes it easier to read and understand. Moreover, often different programmers have different preferred styles of formatting, such as the use of code [[indentation]] and whitespace or positioning of [[Bracket|braces]]. A code formatter 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. | |||
There exist both standalone code beautifiers and ones built into [[integrated development environment]]s and [[text editor]]s. For example, [[Emacs]]' various language modes can correctly [[Indentation|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> | |||
===Lisp pretty-printer=== | |||
An early example of pretty-printing was [[Bill Gosper]]'s "GRIND" program, 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 meaning 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. [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 [http://www.gnu.org/prep/standards/standards.html#Formatting GNU style] and the [http://www.freebsd.org/cgi/man.cgi?query=style&sektion=9 BSD style]. 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 white space also differs. | |||
===Example of formatting and beautifying code=== | |||
The following example shows some typical C structures and how various [[indent style|indentation style]] rules format them. Without any formatting at all, it looks like this: | |||
<source 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;}}} | |||
</source> | |||
The [[indent (Unix)|GNU indent program]] produces the following output when asked to indent according to the [[indent style#GNU style|GNU rules]]: | |||
<source 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; | |||
} | |||
} | |||
} | |||
</source> | |||
It produces this output when formatting according to BSD rules: | |||
<source 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; | |||
} | |||
} | |||
} | |||
</source> | |||
==Text formatting== | |||
[[Text formatting]] can be considered a generalized form of pretty-printing. | |||
==Notes== | |||
<references/> | |||
==See also== | |||
*[[Elastic tabstop]], an [[Indent style|indentation]] technique that leads to automatic pretty-printing in advanced [[source code editor]]s | |||
*[[enscript]], a general text printing tool with prettyprinting functions | |||
*[[indent (Unix)|indent]] | |||
*[[Pretty Diff]] a pretty printer attached to a [[Comparison of file comparison tools|code comparison]], or [[diff]], tool. | |||
== External links == | |||
* [http://portal.acm.org/citation.cfm?id=365673&dl=ACM&coll=ACM Algorithm 268: ALGOL 60 reference language editor] ''W. M. McKeeman'': Commun. ACM 8(11): 667-668 (1965) | |||
* [http://doi.acm.org/10.1145/362790.362796 NEATER2: a PL/I source statement reformatter] ''Kenneth Conrow, Ronald G. Smith'': Commun. ACM 13(11): 669-675 (1970) | |||
* [http://comjnl.oxfordjournals.org/cgi/content/abstract/14/2/133 SOAP - A Program which Documents and Edits ALGOL 60 Programs.] ''R. S. Scowen, D. Allin, A. L. Hillman, M. Shimell'': Comput. J. 14(2): 133-135 (1971) | |||
* [http://www.moorecad.com/standardpascal/prettyp.pas PRETTYP.PAS] Early pascal prettyprinter. ''Ledgard et al.'': [http://www.alibris.com/search/books/qwork/5001951/used/Pascal%20with%20Style:%20Programming%20Proverbs Pascal With Style] (1979) | |||
* [http://www.freebsd.org/cgi/man.cgi?query=style&sektion=9 style(9)] FreeBSD style guidelines | |||
* [http://www.gnu.org/prep/standards/standards.html#Formatting Formatting your source code] GNU style guidelines | |||
[[Category:Articles with example C code]] | |||
[[Category:Source code]] | |||
[[Category:Text editor features]] |
Revision as of 05:55, 13 January 2014
Template:One source Prettyprint (or pretty-print) is the application of any of various stylistic formatting conventions to text, source code, markup, and other similar kinds of content. These formatting conventions usually consist of changes in positioning, spacing, color, contrast, size and similar modifications intended to make the content easier for people to view, read, and understand. Prettyprinters for programming language source code are sometimes called code beautifiers or syntax highlighters.
Pretty-printing mathematics
Pretty-printing usually refers to displaying mathematical expressions in a way that is similar to the way they are typeset professionally. For example, in computer algebra systems such as Maxima or Mathematica the system may write output like "x ^ 2 + 3 * x" as "". Some graphing calculators, such as the Casio 9860 series, HP-49 series, TI-89, and TI-Nspire, or the TI-83 Plus/TI-84 Plus with the PrettyPt add-on, can perform pretty-printing. Additionally, a number of newer scientific calculators are equipped with dot matrix screens capable of pretty-printing such as the Casio FX-ES series (Natural Display), Sharp EL-W series (WriteView), HP SmartCalc 300s, and TI-30XB.
Many text formatting programs can also typeset mathematics: TeX was developed specifically for high-quality mathematical typesetting.
Pretty-printing markup and tag based code
Pretty-printing in markup language instances is most typically associated with indentation of tags and string content to visually determine hierarchy and nesting. Although the syntactical structures of tag based languages do not significantly vary, indentation may vary significantly due to how a markup language is interpreted or in reflect to the data they describe.
In MathML white space characters do not reflect data, meaning, or syntax above what is required by XML syntax. In HTML white space characters between tags are considered text and are parsed as text nodes into the parsed result.[1] While indentation may be generously applied to a MathML document sufficient additional care must be taken in the pretty print operation of a HTML document to ensure additional text nodes are not created or destroyed in general proximity to content or content reflective tag elements. This difference in complexity is non-trivial from the perspective of an automated pretty-print operation where no special rules or edge cases are necessary in the more simple MathML example. The HTML example may require a series of progressive interrelated algorithms to account for various patterns of tag elements and content that conforms to a uniform style and is consistent in application across various instances as evidenced by the markup_beauty.js application component used to beautify HTML, XML, and related technologies for the Pretty Diff tool.
Programming code formatting and beautification
Programmers often use tools to format programming language source code in a particular manner. Proper code formatting makes it easier to read and understand. Moreover, often different programmers have different preferred styles of formatting, such as the use of code indentation and whitespace or positioning of braces. A code formatter 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, loops, etc. (see also control flow), and formatting them in a manner specified by the user in a configuration file.
There exist both standalone code beautifiers and ones built into integrated development environments and text editors. For example, Emacs' various language modes can correctly indent blocks of code attractively.[2]
Lisp pretty-printer
An early example of pretty-printing was Bill Gosper's "GRIND" program, which used combinatorial search with pruning to format LISP programs. Early versions operated on the executable (list structure) form of the Lisp program and were oblivious to the meaning of various functions. Later versions had special read conventions for incorporating non-executable comments and also for preserving read macros in unexpanded form. They also allowed special indentation conventions for special functions such as if
.[3][4]
The term "grind" was used in some Lisp circles as a synonym for pretty-printing.[5]
Project style rules
Many open source projects have established rules for code layout. The most typical are the GNU style and the BSD style. 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 white space 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:
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;}}}
The GNU indent program produces the following output when asked to indent according to the GNU rules:
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;
}
}
}
It produces this output when formatting according to BSD rules:
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;
}
}
}
Text formatting
Text formatting can be considered a generalized form of pretty-printing.
Notes
- ↑ Template:Cite web
- ↑ Template:Cite web
- ↑ Ira Goldstein, "Pretty Printing : Converting List to Linear Structure", Artificial Intelligence Memo 279, Massachusetts Institute of Technology, February 1973. full text
- ↑ Richard C. Waters, "Using the new common Lisp pretty printer", ACM SIGPLAN Lisp Pointers 5:2:27-34, April–June 1992. full text
- ↑ Jargon File, s.v. grind
See also
- Elastic tabstop, an indentation technique that leads to automatic pretty-printing in advanced source code editors
- enscript, a general text printing tool with prettyprinting functions
- indent
- Pretty Diff a pretty printer attached to a code comparison, or diff, tool.
External links
- Algorithm 268: ALGOL 60 reference language editor W. M. McKeeman: Commun. ACM 8(11): 667-668 (1965)
- NEATER2: a PL/I source statement reformatter Kenneth Conrow, Ronald G. Smith: Commun. ACM 13(11): 669-675 (1970)
- SOAP - A Program which Documents and Edits ALGOL 60 Programs. R. S. Scowen, D. Allin, A. L. Hillman, M. Shimell: Comput. J. 14(2): 133-135 (1971)
- PRETTYP.PAS Early pascal prettyprinter. Ledgard et al.: Pascal With Style (1979)
- style(9) FreeBSD style guidelines
- Formatting your source code GNU style guidelines