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

                    indentation in XML </ title>       </ head>       <body>           <h1> indentation in XML < / h1 >           <p>               This document is an example for indentation in XML / SGML.               <br />               In this example, the indent level is 4 spaces.           </ p>       </ body>   </ html> Example: indentation in an Ant source code </p> <p> <? 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 </p> <p> 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. </p> <p> Example: Java source code with indentation depth 2 spaces </p> <p> 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 </p> <p> 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 </p> <p> 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. </p> <h3> Using tab or space </h3> <p> 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. </p> <p> 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. </p> <p> 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. </p> <p> Example: Source with mixed indentation ( both tabs and spaces), which was created with a Tabulatorschrittweite of 4 and 8 displayed </p> <p> 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. </p> <h2> Known indentation styles </h2> <h3> Note on the Examples </h3> <p> The code examples are used to demonstrate the indentation. Your content is of little use. </p> <h3> 1TBS / K & R / kernel / Linux / UNIX / "West Coast " / Stroustrup / Java / Sun </h3> <p> Example: GNU example in 1TBS </p> <p> 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. </p> <p> 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. </p> <p> On the other hand, this style offers two advantages: </p> <ul> <li>The display of many more code in the same number rows. </li> <li>Dependent sequence blocks are recognized at the closing brace under a closing brace in the same column. </li> </ul> <p> 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. </p> <p> 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. </p> <h4> Variation: Original K & R / kernel / Linux / UNIX / Stroustrup </h4> <p> Example: GNU example in K & R </p> <p> 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. </p> <p> 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. </p> <p> 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. </p> <p> The derogation for the functions in C finds several reasons: </p> <ul> <li>Functions can not be nested. </li> <li>K & R also use this exact positioning of the brackets </li> <li>The exemption provides a particularly high readability in wrapped function definitions: </li> </ul> <p> 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: </p> <p> < - Original from http://www.research.att.com/ ~ bs/bs_faq2.html # layout -style - >   class C: public B {   public:           / / ...   }; Variation: Java / Sun </p> <p> Example: GNU example in Java - style </p> <p> 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. </p> <p> Due to the nesting frequently occurring indentation to 4 spaces instead of 8 is preferred. </p> <h3> Allman / BSD / "East Coast " </h3> <p> Example: GNU example in Allman style </p> <p> 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 <a href="/eric-allman.html">Eric Allman</a>, who has written a number of BSD tools, and this style is not quite correct to say BSD - style. </p> <p> 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. </p> <p> Example: advantage of the Allman style in conditional compilation block of discharges </p> <p> < - 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. </p> <p> 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. </p> <h3> Horstmann </h3> <p> 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: </p> <p> 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. </p> <h3> Whitesmith style </h3> <p> 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 ). </p> <p> 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. </p> <h3> GNU - style </h3> <p> Example: Original GNU example </p> <p> 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. </p> <p> The above example was taken from the GNU Coding Standards. The other examples are reformatted in relation to indentation and bracketing. </p> <p> 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. </p> </section> <section class="relLinks"> <a href="/c-sharp-programming-language.html">C Sharp (programming language)</a> <a href="/oberon-programming-language.html">Oberon (programming language)</a> <a href="/apache-ant.html">Apache Ant</a> <a href="/manpage.html">Manpage</a> </section> <div class="comments"> </div> <section> </section> <span style="font-size:.5em">142475</span> <div class="share_buttons"> <div class="addthis_sharing_toolbox"></div> </div> </main> </div> </td></tr><tr><td id="footer"> <div class="aligner"> <footer class="mainHolder" style="text-align:center;"> <!--LiveInternet counter--><script type="text/javascript"><!-- document.write("<a href='http://www.liveinternet.ru/click' "+ "target=_blank rel=nofollow><img src='//counter.yadro.ru/hit?t18.5;r"+ escape(document.referrer)+((typeof(screen)=="undefined")?"": ";s"+screen.width+"*"+screen.height+"*"+(screen.colorDepth? screen.colorDepth:screen.pixelDepth))+";u"+escape(document.URL)+ ";"+Math.random()+ "' alt='' title='LiveInternet' "+ "border='0' width='88' height='31'><\/a>") //--></script><!--/LiveInternet--> <br /> memim.com 2024<br /> All rights reserved<br /> <span style='font-size:0.5em'>Page generated in 0.0032<br /></span> <script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-5093bb6e1c1ddca0"></script> <script> function jr(ready){ if(window.jQuery){ //ready(); }else{ setTimeout(jr,100,ready); } } jr(function() { $(".imagesHolder img").each(function () { var i=$(this); console.log(i.attr('src')+' '+i.width()+' '+i.readyState); //$(this).remove(); }); }); </script> </footer> </div> </td></tr></table> <span style="font-size:.3em">de</span> </body> </html>