Jump to content

Prettyprint

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Dysprosia (talk | contribs) at 06:58, 30 May 2004 (tidy up code sections). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

To prettyprint (or pretty-print) is to make something, commonly some printed material, appear more appealing to the human eye. A prettyprinter, or a program that automatically prettyprints, programming language source code is sometimes called a code beautifier.

Pretty-printing math

Pretty-printing usually refers to displaying mathematical expressions in a way that is similar to the way they are written by hand. For example in computer algebra systems the system may write output like x * x + 3 * x as x2+3x. Mathematical pretty-printing is done by computer algebra programs such as Matlab or Mathematica, as well as by TI-89 graphing calculator. It is usually not done by earlier, less sophisticated graphing calculators like TI-83.

Pretty-printing text

A very familiar idea of prettyprinting is the Wiki markup contributors are using to contribute to the Wikipedia; articles are plain text, for example the above paragraph is internally stored as

Pretty-printing usually refers to displaying mathematical expressions
in a way that is similar to the way they are written by hand. For example in 
[[computer algebra system]]s the system may write output like <tt>x * x + 3 *  x</tt>
as ''x''<sup>2</sup>+3''x''. Mathematical pretty-printing is done by  [[computer algebra]]
programs such as [[Matlab]] or [[Mathematica]], as well as  by [[TI-89]]
[[graphing calculator]]. It is usually not done by earlier, less sophisticated
graphing calculators like [[TI-83]].

However the markup system prettyprints this as you see above.

Code formatting and beautification

Programmers may often want to use automatic tools to format their 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. An automatic code formatter converts source code from one format style to another. This is relatively straightforward because of the very regular nature of the programming languages. Indeed, programming languages are defined in such a way that any particular snippet may have only one meaning, so that it compilation of the source code becomes unambiguous. Code beautification simply 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 built in ones in integrated development environments (IDEs). For example, Microsoft Visual Studio's source code editor does some limited code formatting like indenting blocks of code inside of braces properly.

Example of formatting and beautifying code

int foo(int k) {
  if(k==11) printf("hello\n");
   else printf("good bye\n");
}
int foo(int k)  
{
   if( k == 11 )
     printf("hello\n");
   else 
     printf("good bye\n");
}

int foo(int k) {if(k==11) printf("hello\n"); else printf("good bye\n");}

All three examples mean the same thing and parse in exactly the same manner. Example 1 reflects one preference, example 2 another one, and example 3 is intentionally malformed and unreadable. A code beautifier will convert any of these into a format preferred by the programmer.