open encyclopedia * Article Search: * *
*
*

Prettyprint

From open-encyclopedia.com - the free encyclopedia.

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.

Contents

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 ^ 2 + 3 * x as <math>x^2+3x<math>. Mathematical pretty-printing is done by interactive numerical calculation programs such as Octave (free (as in speech) software) or Matlab, and by computer algebra programs such as Maxima (free (as in speech) software) 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 ^ 2 + 3 * x</tt> as <math>x^2+3x</math>. Mathematical 
 pretty-printing is done by interactive numerical calculation programs 
 such as [[Octave]] {{Free software}} or [[Matlab]], and by 
 [[computer algebra]] programs such as [[Maxima]] {{Free software}} 
 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.

Ironically what a "prettyprinter" produces directly may be decidedly ugly. For instance, the Wiki actually translates to HTML, which may refer to images generated from embedded TeX, which may itself have been created by a math program's "prettyprinter" (see above). None of these files is pretty -- some of it is not even human-readable -- until a web browser (or its equivalent) renders it all.

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 less clear or readable. A code beautifier will convert any of these into a format preferred by the programmer.

Pretty printers

  • GNU Indent
  • GNU Enscript - a printing tool with prettyprinting functions.
  • PHP Code Beautifier [1] - an online tool.
  • Beautify [2] - GPLed software
  • Artistic Style - reformatter and reindenter of C++, C and Java source code
Contribute Found an omission? You can freely contribute to this Wikipedia article. Edit Article
Copyright © 2003-2004 Zeeshan Muhammad. All rights reserved. Legal notices. Part of the New Frontier Information Network.