Code Beautification
   HOME

TheInfoList



OR:

Pretty-printing (or prettyprinting) is the application of any of various stylistic formatting conventions to text files, such as source code, markup, and similar kinds of content. These formatting conventions may entail adhering to an
indentation style In computer programming, an indentation style is a convention governing the indentation of blocks of code to convey program structure. This article largely addresses the free-form languages, such as C and its descendants, but can be (and often ...
, using different color and typeface to highlight syntactic elements of source code, or adjusting size, to make the content easier for people to read, and understand. Pretty-printers for source code are sometimes called code formatters or beautifiers.


Pretty-printing mathematics

Pretty-printing usually refers to displaying mathematical expressions similar to the way they would be typeset professionally. For example, in computer algebra systems such as Maxima or
Mathematica Wolfram Mathematica is a software system with built-in libraries for several areas of technical computing that allow machine learning, statistics, symbolic computation, data manipulation, network analysis, time series analysis, NLP, optimizat ...
the system may write output like "x ^ 2 + 3 * x" as Some graphing calculators, such as the Casio 9860 series,
HP-49 series The HP 49/50 series are Hewlett-Packard (HP) manufactured graphing calculators. They are the successors of the popular HP 48 series. There are five calculators in the 49/50 series of HP graphing calculators. These calculators have both al ...
,
TI-84 Plus The TI-84 Plus is a graphing calculator made by Texas Instruments which was released in early 2004. There is no original TI-84, only the TI-84 Plus, the TI-84 Plus Silver Edition models, and the TI-84 Plus CE. The TI-84 Plus is an enhanced ve ...
, TI-89, and
TI-Nspire The TI-Nspire is a graphing calculator made by Texas Instruments, which was released in July 2007, notable for featuring a non-QWERTY keyboard and a different key-by-key layout than its predecessors. Development The original TI-Nspire ...
, the TI-83 Plus with the PrettyPt add-on, or the
TI-84 Plus The TI-84 Plus is a graphing calculator made by Texas Instruments which was released in early 2004. There is no original TI-84, only the TI-84 Plus, the TI-84 Plus Silver Edition models, and the TI-84 Plus CE. The TI-84 Plus is an enhanced ve ...
with the same add-on or the "MathPrint"-enabled OSes, 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 Sharp or SHARP may refer to: Acronyms * SHARP (helmet ratings) (Safety Helmet Assessment and Rating Programme), a British motorcycle helmet safety rating scheme * Self Help Addiction Recovery Program, a charitable organisation founded in 199 ...
(WriteView), HP SmartCalc 300s, TI-30XB, and Numworks. 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, the indentation may vary significantly due to how a markup language is interpreted or due to the data it describes. In MathML, whitespace characters do not reflect data, meaning, or syntax above what is required by XML syntax. In HTML, whitespace characters between tags are considered text and are parsed as text nodes into the parsed result. While indentation may be generously applied to a MathML document, sufficient additional care must be taken in pretty-printing an HTML document to ensure additional text nodes are not created or destroyed in general proximity to the 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, as 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.ts application component used to beautify HTML, XML, and related technologies for the Pretty Diff tool.


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 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,
loop Loop or LOOP may refer to: Brands and enterprises * Loop (mobile), a Bulgarian virtual network operator and co-founder of Loop Live * Loop, clothing, a company founded by Carlos Vasquez in the 1990s and worn by Digable Planets * Loop Mobile, an ...
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 editors and integrated development environments. For example,
Emacs Emacs , originally named EMACS (an acronym for "Editor MACroS"), is a family of text editors that are characterized by their extensibility. The manual for the most widely used variant, GNU Emacs, describes it as "the extensible, customizable, s ...
' various language modes can correctly indent blocks of code attractively.


HTML


Lisp pretty-printer

An early example of pretty-printing was Bill Gosper's "GRINDEF" (''i.e.'' 'grind function') program (''c.'' 1967), which used combinatorial search with pruning to format
LISP A lisp is a speech impairment in which a person misarticulates sibilants (, , , , , , , ). These misarticulations often result in unclear speech. Types * A frontal lisp occurs when the tongue is placed anterior to the target. Interdental lisping ...
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 macros in unexpanded form. They also allowed special indentation conventions for special functions such as if. The term "grind" was used in some Lisp circles as a synonym for pretty-printing.


Project style rules

Many open source projects have established rules for code layout. The most typical are the GNU formatting and the BSD style.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 In computer programming, an indentation style is a convention governing the indentation of blocks of code to convey program structure. This article largely addresses the free-form languages, such as C and its descendants, but can be (and often ...
rules format them. Without any formatting at all, it looks like this: int foo(int k) The GNU indent program produces the following output when asked to indent according to the GNU rules: int foo (int k) It produces this output when formatting according to BSD rules: int foo(int k)


See also

Related concepts *
Elastic tabstop A tab stop on a typewriter is a location where the carriage movement is halted by an adjustable end stop. Tab stops are set manually, and pressing the tab key causes the carriage to go to the next tab stop. In text editors on a computer, the sam ...
, a feature of many source code editors that detects and maintains aligned indents * Minification, making source code compact, even if it becomes harder for humans to understand *
Obfuscation Obfuscation is the obscuring of the intended meaning of communication by making the message difficult to understand, usually with confusing and ambiguous language. The obfuscation might be either unintentional or intentional (although intent u ...
, deliberately making source code very difficult for humans to understand, even if it becomes somewhat convoluted Utilities * enscript, a text-to-PostScript converter, with pretty-printing features


References

{{reflist


External links


Algorithm 268: ALGOL 60 reference language editor
''
William M. McKeeman William is a male given name of Germanic origin.Hanks, Hardcastle and Hodges, ''Oxford Dictionary of First Names'', Oxford University Press, 2nd edition, , p. 276. It became very popular in the English language after the Norman conquest of Engl ...
'': Commun. ACM 8(11): 667-668 (1965)
lgrind
Comprehensive TEX Archive Network
NEATER2: a PL/I source statement reformatter
''Kenneth Conrow, Ronald G. Smith'': Commun. ACM 13(11): 669-675 (1970)
SOAP - Simplify Obscure Algol Programs
''R. S. Scowen, D. Allin, A. L. Hillman, M. Shimell'': National Physical Laboratory Central Computer Unit repor
CCU6
(April, 1969) Includes formatted listing of SOAP source code. *
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) *
Original SOAP Source Code from the KDF9 (unformatted)

SOAP User's Guide.
(for Edinburgh IMP) ''Peter Salkeld Robertson'' (1976) *
SOAP Source Code
in/for IMP9
Soap80: A Program for Formatting IMP80 Source Programs.
''J.M. Murison, Edinburgh Regional Computer Center'' (1980) *

in/for IMP80 ''E. N. Gregory, University of Kent at Canterbury; Peter D. Stephens, Edinburgh Regional Computer Center''
PRETTYP.PAS
Early pascal prettyprinter. ''Ledgard et al.''
Pascal With Style
(1979)
style(9)
FreeBSD style guidelines

The
nix Nix or NIX may refer to: Places * Nix, Alabama, an unincorporated community, United States * Nix, Texas, a ghost town in southwestern Lampasas County, Texas, United States * Nix (moon), a moon of Pluto People * Nix (surname), listing people with ...
Heirloom Project
Formatting your source code
GNU style guidelines Articles with example C code Source code Text editor features