INDENTATION

[Contents] [Prev] [Next]

Two spaces should be used as the unit of indentation. Never use tabs for indentation.

4.1 Line Length

Avoid lines longer than 80 characters, since they're not handled well by many terminals and tools.

Note: Examples for use in documentation should have a shorter line length-generally no more than 70 characters.

4.2 Wrapping Lines

 When an expression will not fit on a single line, break it according to these general principles:

  • Break after a comma.
  • Break before an operator.
  • Prefer higher-level breaks to lower-level breaks.
  • Align the new line with the beginning of the expression at the same level on the previous line.
  • If the above rules lead to confusing code or to code that's squished up against the right margin, just indent 8 spaces instead.

Here are some examples of breaking method calls:

someMethod(longExpression1,longExpression2,longExpression3, 
           longExpression4,longExpression5);
 
var = someMethod1(longExpression1,
                someMethod2(longExpression2,
                        longExpression3)); 

Following are two examples of breaking an arithmetic expression. The first is preferred, since the break occurs outside the parenthesized expression, which is at a higher level.


longName1 = longName2*(longName3+longName4-longName5)+
            4*longname6; // PREFER

longName1 = longName2 * (longName3 + longName4
                       - longName5) + 4 * longname6; // AVOID 

When method declaration does not fit on one line because of the number of parameters, put one parameter per line instead

someMethod(int anArg,
           Object anotherArg,
           String yetAnotherArg,
           Object andStillAnother) 
{
    ...
}

Here are three acceptable ways to format ternary expressions (but try to evoid using ternary expression):

alpha = (aLongBooleanExpression) ? beta : gamma;  

alpha = (aLongBooleanExpression) ? beta
                                 : gamma;  

alpha = (aLongBooleanExpression)
        ? beta 
        : gamma;  

4.3 Formatting

  • Please ensure that the formatting of the code follows the standard established in FIT standards.
  • Do not use any automatic formatting tools (including the one in IDE) to reformat existing code.

[Contents] [Prev] [Next]