Thursday 17, Apr 2025
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

Related Posts:

  • Java Best Practice – Naming ConventionsNaming conventions make programs more understandable by making them easier to read. They can also give information about the function of the identifier—for example, whether it’s a constant, package, or class—which can … Read More
  • Read File using BufferedInputStream In this tutorials we are going to see how to read file using BufferedInputStream. Create project called JavaMisc. Create package called com.javatutorialscorner.io  Create java class called FileReaderExample… Read More
  • How to convert JSON to Java Object using JacksonIn this Tutorials we are going to see how to convert JSON String to Java object using Jackson data binding. 1. Create project called JSONExample. 2. Create package called com.javatutorialscorner.jackson  3. Cr… Read More
  • How to convert String to Date Time format in JavaIn this tutorial we are going to see how to convert String to Date Time format using SimpleDateFormat. 1. 12-10-2013 12:02:35 PM format to Util Date Timepackage com.javatutorialscorner.util;import java.text.ParseException;i… Read More
  • Java Best Practice - StatementsSimple StatementsEach line should contains one statement.Examplearg1++; //CORRECTarg2++; //CORRECTarg1++;arg2- -; //AVOIDCompound StatementsCompound statemen… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

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