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:
- Break after comma.
- Break before an operator.
- Prefer higher-level breaks to lower level breaks.
- Align new line with the beginning of the expression at same level on previous line.
methodName(longExpression1, 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.
longExpression4, longExpression5 );
var = methodName1(longExpression1, methodName2(longExpression2,
longExpression3));
longName1 = longName2 * (longName3 + longName4 - longName5 )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.
+4 * (longName6); //PREFER
longName1 = longName2 * (longName3 + longName4 - longName5 ) +4 * (longName6); //AVOID
//CONVENTIONAL INDENTATIONLine wrapping for if statement should generally use the 8-space rule, since conventional(4 space)indentation makes seeing the body difficult.For example:
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) {
...
}
//DON’T USE THIS INDENTATIONHere three acceptable ways to format ternary expression:
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();
}
alpha = (aLongBooleanExpression) ? beta : gamma;
alpha = (aLongBooleanExpression) ? beta
: gamma;
alpha = (aLongBooleanExpression)
? beta : gamma;
0 comments:
Post a Comment