Prettyprint

As source code formatting is called the formatting of the source text according to certain rules. Purpose is to improve the code readability, the actual functionality of the programs is not affected.

Code format is included as a function, in some integrated development environments and is thereby used in the generation of source code. There are also stand-alone programs, called source code formatter (English source formatter or beautifier ) that allow the programmer to bring existing code in a good / better readable format.

The use of a source code formatting is especially useful when the programmer must adhere to a predetermined programming style, which requires him how he has to make its source code.

In particular for programming languages ​​that have a C-like syntax, for example, C , Java, C #, the code formatting is widespread.

In the programming languages ​​Python and Occam source code formatting is a syntax element. Blocks are here defined by their indentation, making space get a syntactical meaning.

Examples of source code formatter are the free programs indent and astyle. Even editors like Emacs or Vim have internal appropriate formatting aids, as well as Visual Studio.

Advantages of source code formatting

  • Compliance with a programming style (English code convention)
  • Unification of source code layout
  • Increase the readability
  • Increase the maintainability

The source code formatting aims to the programmers to facilitate the work with sources, especially in the acquisition of source code of other programmers or working in teams. Some quasigenormte variants can be found in the article indentation.

Changes in the source code formatting

  • Standardize the indentation, keeping a certain Einrückungsstils
  • Removing or adding blocks in modifying statements such as if, else, for, while, do, etc.
  • Unification of space before the argument list, such as Space before the arguments modifying instructions
  • No space before the argument lists of function calls
  • No space before the parameter list of a function declaration

More advanced algorithms for source code formatting rule also:

  • Unifying symbol names
  • Renaming of symbols in accordance with conventions, eg m_MainControl in main control
  • Addition of hulls for Documentation Comments
  • Taking on tasks of code analysis tools, to the proposal of refactorings

Examples of the operation of

Output source code:

For (int i = 0; i < 10; i )      if ( values ​​[ i] > 0 && values ​​[ i 1 ]> 0)      {              out.println ( " found! ");   break;      } Result of formatting:

For (int i = 0; i < 10; i ) {       if ( values ​​[ i] > 0 && values ​​[ i 1 ]> 0) {           out.println ( " found! ");           break;       }   } What has changed?

  • Basically blocks behind for, if, etc. even if individual statements are allowed ( allows easy complete instructions in the block)
  • The indentation was unified consistently ( over 4 per space)
  • Opening brace on the same line (a common style in Java and C )
  • Fanning spaces were set strictly according to the rules: behind keywords such as if, while ...
  • No space after opening and before closing round bracket (analog square brackets )
  • Space between binary operators ( those with two operands)

Even if the style shown here - for example, in the Java programming - is relatively common, there are a variety of other styles that are partly justified by the tradition, and partly by the properties of the respective programming language.

Disadvantages of the use

Programs that perform the source code formatting, are not capable of such people to grasp the meaning of a source text. The rules for reformatting are very schematic and expressionless. Consciously made ​​, the purpose of the course serves violations underlying the Umformatierungsregeln underlying conventions are lost. example:

Int width = minwidth * 2 padding * 4; will under certain circumstances

Int width = minwidth * 2 padding * 4; The violation of the Convention of the programmer obviously served the purpose of increasing the intelligibility of the program by bringing in comparison to the addition ( ) by the omission of spaces the higher priority of multiplication ( *) expression. This is not relevant for the program but for a programmer read information was lost in the source code formatting. If the operator precedence is not clearly identifiable, instead brackets should be put through this also errors can be avoided by incorrectly assumed rankings.

Another disadvantage is often cited that source code formatting destroy the " personal style " of a programmer. This can usually be opposed, that it does not arrive in team programming on the personal style and the ego of individuals but that the team a pleasant and speedy work should be allowed as a whole. This means that to dispense with personal style and to be followed a valid control for the entire team.

111071
de