White Space Usage

White Space Usage

Blank Lines

Blank lines can improve readability by grouping sections of the code that are logically related . A blank line should also be used in the following places:

  1. after the copyright block comment, package declaration, and import section;

  2. between class declarations;

  3. between method declarations;

  4. between the last field declaration and the first method declaration in a class; and

  5. before a block or single-line comment, unless it is the first line in a block.

Blank Spaces

A single blank space (not tab) should be used:

  1. between a keyword and its opening parenthesis. This applies to the following keywords: catch, for, if, switch, synchronized while. It does not apply to the keywords super and this;

  2. after any keyword that takes an argument. Example: return true;

  3. between two adjacent keywords;

  4. between a keyword or closing parenthesis, and an opening brace "{";

  5. before and after binary operators except .(dot);

  6. after a comma in a list; and

  7. after the semicolons in a for statement, e.g.:

     for (expr1; expr2; expr3) {. 

Blanks should not be used:

  1. between a method name and its opening parenthesis;

  2. before or after a .(dot) operator;

  3. between a unary operator and its operand;

  4. Between a cast and the expression being casted;

  5. After an opening parenthesis or before a closing parenthesis; and

  6. After an opening square bracket [ or before a closing square bracket ].

Examples:

 a += c[i + j] + (int)d + foo(bar(i + j), e);        a = (a + b) / (c * d);        if (((x + y) > (z + w))  (a != (b + 3))) {            return foo.distance(x, y);        } 

Do not use special characters like form-feeds or backspaces.

Indentation

Line indentation is always 4 spaces, for all indentation levels.

The construction of the indentation may include tabs as well as spaces in order to reduce the file size ; however, you may not change the hard tab settings to accomplish this. Hard tabs must be set every 8 spaces.

Continuation Lines

Lines should be limited to 80 columns (but not necessarily 80 bytes, for non-English languages). Lines longer than 80 columns should be broken into one or more continuation lines, as needed. All the continuation lines should be aligned, and indented from the first line of the statement. The amount of the indentation depends on the type of statement.

If the statement must be broken in the middle of a parenthesized expression, such as for compound statements, or for the parameter list in a method invocation or declaration, the next line should be aligned with the first character to the right of the first unmatched left parenthesis in the previous line. In all other cases, the continuation lines should be indented by a full standard indentation (4 spaces). If a class or method declaration has one or more continuation lines, then a single blank line should immediately follow the opening brace.

Examples:

 if (long_logical_test_1  long_logical_test_2              long_logical_test_3) {            statements;         }        function(long_expression1, long_expression2,                  long_expression3, long_expression4,                       long_expression5, long_expression6); 

A continuation line should never start with a binary operator. Never break a line where normally no white space appears, such as between a method name and its opening parenthesis, or between an array name and its opening square bracket. Never break a line just before an opening brace {. Examples:

 // WRONG        while (long_expression1  long_expression2                 long_expression3)        {        }        // RIGHT        while (long_expression1  long_expression2                long_expression3) {        } 


Software Development. Building Reliable Systems
Software Development: Building Reliable Systems
ISBN: 0130812463
EAN: 2147483647
Year: 1998
Pages: 193
Authors: Marc Hamilton

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net