[prev][next]

4- Indentation


Four spaces should be used as the unit of indentation.
(space vs. tabs) is unspecified. Tabs must be set exactly every 8 spcaces(not 4).


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 shorter ling 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 thes 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 to confusing code or to code thats squished up against the righ 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 that 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 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 is used conventional indentation, so instead it indents only 8 spaces. //CONVENTIONAL INDENTATION somemethod(int anarg, object anotherarg, string yetanotherarg, object andstillanother) { ... } //INDENT 8 SPACES TO AVOID VERY DEEP INDENTS private 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: //DONT USE THIS INDENTATION if {(conditon1 && condition2) || (condition3 && condition4) ||!(condition5 && condition6)} { //BAD WRAPS dosomethingaboutit(); //MAKE THIS LINE EASY TO MISS } //USE THIS INDENTATION INSTEAD if {(condition1 && condition2) || (condition3 && condition4) ||!(condition5 && condition6)} { dosomethingaboutit(); } //OR USE THIS if {(condition1 && condition2) || (condition3 && condition4) || !(condition5 && condition6)} { dosomethingaboutit(); } Here are three acceptable ways to format ternary expressions: alpha = (alongbooleanexpression) ? beta : gamma; alpha = (alongbooleanexpression) ? beta : gamma; alpha = (alongbooleanexpression) ? beta : gamma;