Thursday, February 24, 2011

White Space

White Space

Blank lines improve readability by setting off sections of code that are logically related.

Two blank lines should always be used between sections of a source file. That is, two blank lines should follow:

·         The opening comment

·         The package statement

·         The import statements

·         The class and interface declaration

One blank line should always be used in the following circumstances:

·         Between methods

·         Between the local variables in a method and its first statement

·         Before a block or single-line comment.

·         Between logical sections inside a method to improve readability.

·         Use common sense and don't be afraid to put blank lines!

Blank Spaces

  • Blank spaces should be used in the following circumstances:
  • A blank space should appear after commas in argument lists.
  • All binary operators except. Should be separated from their operands by spaces. Blank spaces should never separate unary operators such as unary minus, increment ("++"), and decrement ("--") from their operands.
  • The expressions in a "for" statement should be separated by blank spaces.

Example:

public void aMethod(int a, int b, int c, int d)
{
    a += c + d;
    a = (a + b) / (c * d);
 
    printSize("size is " + foo);
 
    for(expr1; expr2; expr3)
    {
        /* No Body */
     }
}

 

 

 
 

 

 

 

 

 


Parentheses

It is generally a good idea to use parentheses liberally in expressions involving mixed operators to avoid operator precedence problems

if(a == b && c == d)        // AVOID!

if((a == b) && (c == d))    // RIGHT

 

 

 
 

 


--


No comments:

Post a Comment