We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 14 September 2013

Java Best Practice - Indentation


Four space should be used as the unit of indentation.The exact construction of the indentation(space vs tab) is unspecified.Tabs must be set exactly every 8 spaces(not 4).
Line Length
Avoid line length longer than 80 characters, since they are not handled well by many terminals and tools.
Wrapping Lines
When an expression will not fit on single line,break it according to these general principles:
  1. Break after comma.
  2. Break before an operator.
  3. Prefer higher-level breaks to lower level breaks.
  4. Align new line with the beginning of the expression at same level on previous line.
Here some examples given for breaking the method calls.
methodName(longExpression1, longExpression2, longExpression3,

longExpression4, longExpression5 );

var = methodName1(longExpression1, methodName2(longExpression2,

longExpression3));
Following two examples of breaking an arithmetic expression.The first is prepared, since the break occuers outside of parenthesized expression, which is at a higher level.
longName1 = longName2 * (longName3 + longName4 - longName5 )

+4 * (longName6); //PREFER

longName1 = longName2 * (longName3 + longName4 - longName5 ) +4 * (longName6); //AVOID
Following are two example of indenting methods declaration.The first is the conventional case.The second would shift the second and third lines to far right if it used conventional indention, so instead it indents only 8 spaces.
//CONVENTIONAL INDENTATION

methodName(int anArg, Object anotherArg, String yetAnotherArg,

Object andStillAnother) {

...

}

//INDENT 8 SPACES TO AVOID VERY DEEP INDENTS

private static static synchronized horkingLongMehodName(int anArg,

Object anotherArg, String yetAnotherArg,

Object andStillAnother) {
...

}
Line wrapping for if statement should generally use the 8-space rule, since conventional(4 space)indentation makes seeing the body difficult.For example:
//DON’T USE THIS INDENTATION

if ((condition1 && condition2)

|| (condition3 && condition4)

||!(condition5 && condition6)) { //BAD WRAPS

//MAKE THIS LINE EASY TO MISS

doSomething();

}

//USE THIS INDENTATION INSTEAD

if ((condition1 && condition2)

|| (condition3 && condition4)

||!(condition5 && condition6)) {

doSomething();

}

//OR USE THIS

if ((condition1 && condition2) || (condition3 && condition4)

||!(condition5 && condition6)) {

doSomething();

}
Here three acceptable ways to format ternary expression:
alpha = (aLongBooleanExpression) ? beta : gamma;

alpha = (aLongBooleanExpression) ? beta
                                                     : gamma;
alpha = (aLongBooleanExpression)
            ? beta             : gamma;

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Java Best Practice - Indentation Rating: 5 Reviewed By: eHowToNow