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){ |
The above code can be rewritten as
1 |
private boolean isEligible( int age){ |
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){ |
We can use remove if-else
1 |
private boolean isEligible( int age){ |
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:
We should write
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
can be rewritten as
1 |
if (age > 18 && !voted){ |
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); |
5 |
logger.log( "My Application" + f); |
we can create a constant to replace "My Application"
1 |
private void someMethod(){ |
2 |
logger.log( "My Application" + e); |
5 |
logger.log( "My Application" + f); |
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