Today's Question:  What does your personal desk look like?        GIVE A SHOUT

Writing Java codes conforming to coding standard

  sonic0002        2012-09-18 12:50:28       5,705        0    

Recently, I was doing some cleanup to one of my current Java project. I find there are many codes which are not conforming to the Java coding standard. So I list them here and hope that people can improve your codes and write maintainable codes.

Format source code and manage imports in Eclipse

Eclipse provides functions of auto-formatting and imports management, you can use following shortcuts to use these functions.

  • Ctrl+Shift+F --> Format source code
  • Ctrl+Shift+O -- Manage imports and remove unused import statements

In addition to manually execute these two functions, you can also let Eclipse auto format your source codes and manage your imports when you save the source codes. In order to achieve this, go to Windows->Preference->Java->Editor->Save Actions, and select the Perform the selected actions on save, check Format source code and organize imports.

Avoid multiple return statements in methods(multiple exit points)

In your method, make sure there is only one exit point, don't put multiple return statements in a method.

For example, the following code snippet is not recommended.

1 private boolean isEligible(int age){
2   if(age > 18){
3     return true;
4   }else{
5     return false;
6   }
7 }

The above code can be rewritten as

1 private boolean isEligible(int age){
2   boolean result;
3   if(age > 18){
4     result = true;
5   }else{
6     result = false;
7   }
8   return result;
9 }

 

Simplify if-else

We may often need to write some methods which take only one argument and check on that argument and return some value according to the argument.

For example the isEligible() method above :

1 private boolean isEligible(int age){
2   boolean result;
3   if(age > 18){
4     result = true;
5   }else{
6     result = false;
7   }
8   return result;
9 }

 

We can use remove if-else

1 private boolean isEligible(int age){
2   return age > 18;
3

}

Don't create new instance for Boolean, Integer and String

Avoid creating instance of Boolean, Integer and String with new, use Boolean.valueOf(true) to replace new Boolean(true). These two methods have the same effect but have different performance.

Use {} around code block

Don't forget to put {} around code block (such as if, while,for), this can reduce code confusion and avoid introducing new bugs when changing codes in the future.

Following code snippet is not recommended:

1 if(age > 18)
2   return true;
3 else
4   return false;

We should write

1 if(age > 18){
2   return true;
3 }else{
4   return false;
5 }

Declare method parameter as final

Declare method parameter as final whenever possible, by doing so, when you change the value of the parameter accidently, the compiler may throw an error. Also, the machine codes generated with final will also be optimized.

1 private boolean isEligible(final int age){ ... }

Give CAPITAL name for public static final variables

Always use CAPITAL name as public static final variable name, this can distinguish what is constant and what is local variable.

Following is not recommended:

1 public static final String testAccountNo = "12345678";

Follwong is recommended

1 public static final String TEST_ACCOUNT_NO = "12345678";

Merge multiple if statements

The following code snippet

1 if(age > 18){
2   if( voted == false){
3     // eligible to vote.
4   }
5 }

can be rewritten as

1 if(age > 18 && !voted){
2   // eligible to vote
3 }

Don't forget to add default statement for switch

Always add a default statement for switch

Avoid repetitively using the same string literal, create a constant instead

If you need to use the same string literal in many places, then the wise way is to create a string constant.

For example

1 private void someMethod(){
2   logger.log("My Application" + e);
3   ....
4   ....
5   logger.log("My Application" + f);
6 }

we can create a constant to replace "My Application"

1 private void someMethod(){
2   logger.log("My Application" + e);
3   ....
4   ....
5   logger.log("My Application" + f);
6 }

Writing correct program is just part of programming, we should develop good programming habits in order to improve coding efficiency. Writing correct codes is not enough, we have to consider maintainebility and readability because frequently other people may maintain our codes. We as programmer don't want to hear something "What a stupid programmer" from others, right? So writing codes conforming to standard  both for yourself and for others.

Reference : http://www.oschina.net/question/100267_70351

JAVA  STYLE  CODE STANDARD 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  0 COMMENT


No comment for this article.