Indentation
8 spaces should be used as the unit of indentation.
Line Length
Avoid lines longer than 80 characters.
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:
function(longExpression1, longExpression2, longExpression3,longExpression4, longExpression5);var = function1(longExpression1,function2(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; // PREFERlongName1 = longName2 * (longName3 + longName4- longName5) + 4 * longname6; // AVOID
Following are two examples of indenting method declarations. The first is the conventional case. The second would shift the second and third lines to the far right if it used conventional indentation, so instead it indents only 8 spaces.
//CONVENTIONAL INDENTATIONsomeMethod(int anArg, Object anotherArg, String yetAnotherArg,Object andStillAnother) {...}//INDENT 8 SPACES TO AVOID VERY DEEP INDENTSprivate static synchronized horkingLongMethodName(int anArg,Object anotherArg, String yetAnotherArg,Object andStillAnother) {...}
Line wrapping for if
statements should generally use the 8-space rule, since conventional (4
space) indentation makes seeing the body difficult. For example:
//DON’T USE THIS INDENTATIONif ((condition1 && condition2)|| (condition3 && condition4)||!(condition5 && condition6)) { //BAD WRAPSdoSomethingAboutIt(); //MAKE THIS LINE EASY TO MISS}//USE THIS INDENTATION INSTEADif ((condition1 && condition2)|| (condition3 && condition4)||!(condition5 && condition6)) {doSomethingAboutIt();}