Source code formatting

from Wikipedia, the free encyclopedia

As prettyprint formatting is called a source text according to certain rules. The purpose is to improve the readability of the program code, the actual functionality of the programs is not affected.

Source code formatting is included as a feature in some integrated development environments and is used when generating source code. There are also independent programs , so-called source formatters ( also known as source formatters or beautifiers ), which enable the programmer to convert existing source text into a format that is easy to read or easier to read.

The use of source code formatting is particularly useful when the programmer has to adhere to a given programming style that dictates how he should design his source code.

Especially with programming languages ​​that have a C -like syntax , e.g. B. C ++ , Java , C # , the source code formatting is widespread.

In the programming languages F # , Elm , Python , Occam, and a few others, source code formatting is a syntax element. Blocks are defined here by their indentation, which gives spaces a syntactic meaning.

Examples of source code formatters are the free programs indent and astyle . Even editors like Emacs or Vim internally have adequate Formatting Aids, as well as Visual Studio .

Benefits of source code formatting

  • Compliance with a programming style ( code convention )
  • Standardization of the source text layout
  • Increase in readability
  • Increase in maintainability

The aim of formatting the source text is to make it easier for programmers to work with source texts, especially when adopting source texts from other programmers or when working in teams. Some quasi-standardized variants can be found in the article indentation style .

Source code formatting changes

  • Unification of indentation, adherence to a certain indentation style
  • Removing or adding blocks in modifying statements such as if, else, for, while, doetc.
  • Standardization of spaces in front of the argument list, e.g. B.
    • Spaces in front of the arguments of modifying statements,
    • no spaces before the argument lists of function calls or
    • no spaces in front of the parameter list of a function declaration.
  • consistently uniform positioning of the {} for blocks
  • Insertion of () according to the operator priority for operators with generally unclear priorities

More advanced algorithms for source code formatting also master:

  • Standardization of symbol names
  • Renaming of symbols according to conventions, e.g. B. m_MainControl in mainControl
  • Addition of hulls for documentation comments
  • Assumption of tasks from code analysis tools to the suggestion of refactorings

Examples of how it works

Source code:

   for (int i=0;i<10;i++)
    if( values[i]>0&&values[ i+1 ]>0 )
            out.println ("found!");

Result of the formatting:

for (int i = 0; i < 10; i++) {
    if (values[i] > 0 && values[i + 1] > 0) {


  • Basically blocks after for, if u. similar even if individual instructions are allowed (allows easy addition of instructions in the block)
  • The indentation has been consistently standardized (to 4 spaces each)
  • opening curly bracket on the same line (a common style in Java and C ++)
  • loosening spaces were set strictly according to rules:
    • behind keywords like if, while ...
    • no space after opening and before closing round brackets (analogous to square brackets)
    • Space between binary operators (those with two operands)

Even if the style shown here - e.g. B. in Java programming - is relatively common, there are a variety of other styles, some of which are based on tradition and partly on the characteristics of the programming language in question.

Disadvantages of use

Programs that do the source text formatting are not able to grasp the meaning of a source text like humans. The rules for reformatting are very schematic and expressionless. Deliberately made violations of the conventions on which the reformatting rules are based for the purpose of clarity are lost. Example:

int width = minwidth*2 + padding*4;

may become too

int width = minwidth * 2 + padding * 4;

The programmer's violation of the convention obviously served the purpose of making the program more understandable by expressing the higher priority of multiplication (*) compared to addition (+) by omitting spaces. This information, which is not relevant for the program but for a reading programmer, was lost due to the source text formatting. If the operator precedence is not clearly recognizable, brackets should be used instead; these can also avoid errors due to incorrectly assumed precedence.

Another disadvantage is often cited that source code formatting destroys the "personal style" of a programmer. This can usually be countered by the fact that programming in a team does not depend on the personal style and ego of individuals, but on the fact that the team as a whole should be able to work comfortably and quickly. This means that you have to forego personal style and follow a rule that applies to the entire team.

Individual evidence

  1. ^ Code Conventions for the Java Programming Language. In: , Sun Microsystems , April 20, 1999. (English)