Definition

prettyprint

Prettyprint is the process of converting and presenting source code or other objects in a legible and attractive way. A prettyprinter takes blocks of code and prints them in an aesthetically pleasing fashion, presenting the characters with line breaks and indentations to make the code comprehensible. Prettyprinters are also called code beautifiers.

Code formatting

Prettyprint creates a representation of source code that can be easily analyzed by the interpreter as well as easily read by humans. Code formatting splits blocks of code into individual lines.

When formatting the lines to make them more attractive, the prettyprinter might use indentation style or syntax highlighting. Indention style refers to the process of adjusting the positioning and spacing of lines of code. Indenting commands can help make the structure of the program more legible to human readers.

Syntax highlighting is the process of adding color or contrast to lines of code. Different colors or fonts are used to specify the category of the term. This feature reveals structure as well as syntax errors. The different colors or fonts will have no effect on the code, it is only used to make the text more clear and accessible to humans.

The prettyprinter should try to keep all objects in one line if it will fit. If it cannot, then the arguments -- independent variables that contain pieces of data or code -- should be listed directly under one another and indented.

Methods

Python is an interpreted, object-oriented programming (OOP) language that already utilizes indentation style to make its text easier to read. The Python pprint module is one of the more popular methods for prettyprinting code. The pprint module allows users to take the input of chaotic Python data structures and present the output in a readable form. The pprint module also provides various shortcut functions such as pprint.pprint. This shortcut prints the formatted representation of an object on stream -- a primitive used to send and receive data without the use of callbacks or low-level protocols and transports -- and followed by a newline -- a control character or sequence of control characters that are used to mark the end of a line of text and start a new one.

The pprint module can also be used to prettyprint Javascript Object Notation (JSON), a text-based, legible data formatter that represents simple data structures and objects in web browser based code. JSON does not include mathematical notation or algorithms and presents a simple way to represent data structures. 

Computer programmers have also created their own algorithms and libraries to prettyprint source code. These homemade prettyprinters can be found all over the web and are usually designed to accommodate to the specific needs of the creator.

Advantages and disadvantages

Prettyprint is advantageous to users because it makes source code easier to read and increases the ability to reuse strings. However, it will also increase the size of the data, potentially making the program slower, and will also produce computational overhead -- excess or indirect computation resources that are required to perform a task.

This was last updated in June 2019

Continue Reading About prettyprint

Dig Deeper on Software development best practices and processes