Indentation
As indentation (English indent style) is the way referred to indent text of programs to improve readability and to position enclosing syntax elements such as braces { }. As an alternative name, therefore, is sometimes " clamp style " (English brace style) to be found.
For the C programming language, there are four common indentation styles, like C , Java, JavaScript, Perl or C # have been taken also in programming and scripting languages with C- like syntax.
The positioning of the braces is probably the most controversial element of a programming style.
- 2.1 Note on Examples
- 2.2 1TBS / K & R / kernel / Linux / UNIX / "West Coast " / Stroustrup / Java / Sun 2.2.1 Variation: Original K & R / kernel / Linux / UNIX / Stroustrup
- 2.2.2 Variation: Java / Sun
Elements of Einrückungsstils
The indentation style applies to:
- Positioning enclosing syntax elements, in particular { } and ()
- Depth of the indentation
- Use of spaces before and { (
- Using tab- indentation.
Often it is recommended not to format a source such that the indentation only with a change in the Tabulatorschrittweite to a different value is visible, eg 4 instead of 8 characters. The vast majority of editors move the default to the tab by 8 spaces. To avoid problems in the representation in terms entirely on Tabulatorschrittweite, advise most indentation styles from the use of tab characters on principle.
Positioning enclosing syntax elements
Enclosing syntax elements are those syntax elements of a language that are used to group an indefinite number of elements, not exclusively but especially when the elements extend over several lines of code. In languages with C- like syntax, for example, C , Java and C #, in languages with Pascal - like syntax, such as Modula-2, Oberon and clusters as well as some other languages these elements falls in the design of a source text in terms of its syntax and its readability is of central importance.
Positioning in Pascal
Pascal -like languages use to enclose specially defined keywords, mostly BEGIN, DO and END. In practice, the positioning of these elements is seldom discussed, most of it is carried out as demonstrated by Niklaus Wirth in the literature. It BEGIN yet in the indentation of the outer block on a separate line, DO, however, is written at the end of the previous line, together with the DO -requiring the statement. The content of the introduced by BEGIN or DO block is indented exclusively to the END. The END is disengaged, that is, in the indentation of the outer block finally back on its own line.
Example: Positioning of the enclosing syntax elements in Oberon
PROCEDURE Add ( VAR x, y: INTEGER) BEGIN WHILE ( y! = 0) DO y: = y - 1; x: = x 1; END Add END; Positioning in C.
C-like languages use to enclose a pair of curly braces { and }. In practice, these brackets can be positioned almost arbitrarily, so that several different styles have developed, none of which can be described as dominant.
Positioning in XML and SGML - based languages
In XML and SGML - based languages , such as HTML, has prevailed to engage the content of elements between start tag and end tag. For attributes are becoming increasingly common a multi-line layout, especially when clarity is needed.
Example: indentation in a XHTML source code
xml version = " 1.0"? >
indentation in XML < / h1 >
This document is an example for indentation in XML / SGML.
In this example, the indent level is 4 spaces.
p>
body>
html>
Example: indentation in an Ant source code
xml version = " 1.0"? > < - - Daimonin editor build file -> < project name = " Daimonin Editor" default = " compile" > < target name = "compile " description = "compile Daimonin Editor" > < javac srcdir = " src" destdir = "classes " encoding = "utf -8" source = "1.5" target = "1.5" debug = "no" / > < / target> project > Indentation
The indentation width determines how far the content of a block is indented. Especially prevalent are indenting by 4 or 8 spaces, but also 2 or 3 spaces is not rare. Ultimately, the indentation depth a compromise between a large indentation depth by the clearest possible visibility of the indentation on the one hand and the largest possible resultant line length on the other hand represents the line length obtained can be calculated as follows: received line length = length - * nesting indent level. With a line length of 80 characters, one shiftwidth of 4 spaces and a nesting depth of 3 levels, the line length so obtained is still 68 characters.
Example: Java source code with indentation depth 2 spaces
Public class Hello { public static void main ( string args .. ) { if ( args.length > 0) { for ( String arg: args) { System.out.println ("Hello, " arg "!"); } Else { } System.out.println ("Hello, world! "); } } } Example: Java source code with 4 spaces indentation depth
Public class Hello { public static void main ( string args .. ) { if ( args.length > 0) { for ( String arg: args) { System.out.println ("Hello, " arg "!"); } Else { } System.out.println ("Hello, world! "); } } } Example: Java source code with indentation, 8 spaces
Public class Hello { public static void main ( string args .. ) { if ( args.length > 0) { for ( String arg: args) { System.out.println ("Hello, " arg "!"); } Else { } System.out.println ("Hello, world! "); } } } As you can see, increases with increasing indentation depth, the clear visibility of the indentation, but the lines are longer, which leaves less room for expressions within a row with a limited text width.
Using tab or space
The indentation of the source code can do in most programming languages either with spaces or with tabs. Most compilers will see no difference, so the choice is left to the programmer.
However, there are arguments for and against an indent with spaces or tabs. For an indentation with spaces is the fact that the indentation depth remains basically the same and the indentation is always visible, regardless of which Tabulatorschrittweite used. For an indentation with tab says that any developer can even set the Tabulatorschrittweite and thus determine his personal shiftwidth.
More important than the choice of Einrückungsstils to consistently comply with the chosen style, as a mixture to problems in the representation can cause is.
Example: Source with mixed indentation ( both tabs and spaces), which was created with a Tabulatorschrittweite of 4 and 8 displayed
Public class Hello { public static void main ( string args .. ) { if ( args.length > 0) { for ( String arg: args) { System.out.println ("Hello, " arg "!"); } Else { } System.out.println ("Hello, world! "); } } } The representation could be corrected by a change of Tabulatorschrittweite. For a few editors, such as vim, can be put in the source code control information, which provide the editor automatically on the Tabulatorschrittweite used. However, these editors are the exception, and there is a lack of a uniform standard for such settings. Without these options, the programmer needs in mixed source code each source optically consider to guess the Tabulatorschrittweite and accordingly adjust.
Known indentation styles
Note on the Examples
The code examples are used to demonstrate the indentation. Your content is of little use.
1TBS / K & R / kernel / Linux / UNIX / "West Coast " / Stroustrup / Java / Sun
Example: GNU example in 1TBS
If ( x < foo ( y, z )) { qux = bar 5; Else { } while ( Z) { qux = foo ( z, z ); z -; } return x bar (); } The name comes from the 1TBS hacker jargon stands for " One True Brace Style", which translated means " the only true style brackets ". The name refers to the fact that this style has been defined by the C- inventors Brian W. Kernighan and Dennis Ritchie.
The position of the opening parenthesis at end of line is equally popular as hateful. With poor development tools is due to carelessness the risk of overlooking open brackets and forget closing.
On the other hand, this style offers two advantages:
- The display of many more code in the same number rows.
- Dependent sequence blocks are recognized at the closing brace under a closing brace in the same column.
The vast majority of Java and ECMAScript programmer uses this style. Even Perl programmers usually use 1TBS because they prefer it anyway to keep their programs as short as possible.
C code used earlier 8 spaces per Einrückungsschritt, today 4 or 3 spaces are also sometimes encountered. In Java and C indentation to 4 spaces is the rule.
Variation: Original K & R / kernel / Linux / UNIX / Stroustrup
Example: GNU example in K & R
Int f ( int x, int y, int z) { if ( x < foo ( y, z )) { qux = bar 5; Else { } while ( Z) { qux = foo ( z, z ); z -; } return x bar (); } } This programming style has been from the C Brian W. Kernighan and inventors Dennis Ritchie (K & R ) in their book The C Programming Language ( German title: Programming in C ), and together with Ken Thompson used in the development of UNIX and its kernel. The Code Conventions by Linus Torvalds for Linux suggest that style.
The indentation depth shown is that used in the Linux kernel. The indentation of subordinate syntax elements is there 8 spaces. This facilitates the readability even after 20 hours of computer work. From an indentation of more than 3 levels is not recommended and instead proposed a restructuring of the source code.
In contrast to the consistent use of 1TBS, such as the Java - style, is being introduced in the definition of functions on the Allman style.
The derogation for the functions in C finds several reasons:
- Functions can not be nested.
- K & R also use this exact positioning of the brackets
- The exemption provides a particularly high readability in wrapped function definitions:
Static int kwrite_args (struct posix_log_entry rec_hdr *, va_list args, unsigned char ** pvardata ) { / / ... } If this style of C used, it is also called Stroustrup after the inventor of C , which has expanded the K & R style to elements of C . For all new elements, such as classes, braces are placed at the end:
< - Original from http://www.research.att.com/ ~ bs/bs_faq2.html # layout -style - > class C: public B { public: / / ... }; Variation: Java / Sun
Example: GNU example in Java - style
Int f ( int x, int y, int z ) { if ( x < foo ( y, z )) { qux = bar 5; Else { } while ( Z) { qux = foo ( z, z ); z -; } return x bar (); } } This style is recommended by Sun for the Java programming language. Even in C , this style is often encountered. On the exemption to position the braces of methods ( = object-oriented function ) according to Allman, was not made because methods are nested as members of classes.
Due to the nesting frequently occurring indentation to 4 spaces instead of 8 is preferred.
Allman / BSD / "East Coast "
Example: GNU example in Allman style
Int f ( int x, int y, int z) { if ( x < foo ( y, z) ) { qux = bar 5; } else { while ( Z) { qux = foo ( z, z ); z -; } return x bar (); } } This style was named after Eric Allman, who has written a number of BSD tools, and this style is not quite correct to say BSD - style.
Is regarded by experienced programmers as a disadvantage that this style has a high consumption line. An additional line is required in comparison with 1TBS per block. In C, C and languages with comparable preprocessors however, there are situations in which this style is also significant advantages over 1TBS shows, namely the use of conditional compilation block for alternative discharges.
Example: advantage of the Allman style in conditional compilation block of discharges
< - Quote from the main.cpp Daimonin 3D clients, can be changed. -> # if == OGRE_PLATFORM OGRE_PLATFORM_WIN32 INT WINAPI WinMain ( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT ) # else int main (int argc, char ** argv ) # endif { / / ... } The total number of brackets remains even when using conditional compilation equal, which is important for the correct assignment of opening and closing brackets in text editors with appropriate functionality.
Note: This style is indeed often called BSD - style, however, is for the BSD kernel as used for UNIX and Linux 1TBS, as can be read in the Style Manual pages for the various BSD derivatives.
Horstmann
The Horstmann style is a variation of the Allman style, in which the first statement after the opening brace is written directly in the same line. The code above looks like this:
Int f ( int x, int y, int z) {If ( x < foo ( y, z) ) { Qux = bar 5; } else {While ( z) { Qux = foo ( z, z ); z -; } return x bar (); } } For VCS tools, this style is problematic. If you add a new statement between the "{" and the first statement one, this change appears as " a line is replaced by 2 lines " instead of " a new row was inserted ." That is, the original line is marked as "changed", although it has remained semantically equal.
Whitesmith style
The Whitesmith style was originally used in the documentation of the first commercial C compiler. This style was popular in early Windows programming books, including the Programmer's Guide to Windows ( Durant, Carlson & Yao ) in Programming Windows ( Petzold ) and Windows 3.0 Power Programming Techniques ( Norton & Yao ).
Int f ( int x, int y, int z) { if ( x < foo ( y, z) ) { qux = bar 5; } else { while ( Z) { qux = foo ( z, z ); z -; } return x bar (); } } The advantage of this style is that the block differentiating it from the control statement, and unlike Allman style does not interrupt the flow of reading the block.
GNU - style
Example: Original GNU example
Int f ( int x, int y, int z) { if ( x < foo ( y, z) ) qux = bar 5; else { while ( Z) { qux = foo ( z, z ); z -; } return x bar (); } } This programming style is found mainly in the Free Software Foundation GNU project and is recommended by you. Outside the Free Software Foundation, he is rather rare and also within the Free Software Foundation, he is not used by everyone.
The above example was taken from the GNU Coding Standards. The other examples are reformatted in relation to indentation and bracketing.
Therefore advocate of GNU - style describe him as very readable because the braces in the positioning / indentation plays its own layer. The other hand, opponents argue that this arises a certain chaos at the source code formatting; readability and consistent indentation would be more difficult.