9 common non-standard Java code

Source: Internet
Author: User

At work, I recently cleaned up an existing Java project code. After that, I found some repeated nonstandard code. So I organized them into a list and shared them with my peers, hoping to attract attention and improve the quality and maintainability of the Code.
This list is not ordered, and all comes from some code quality check tools, such as CheckStyle, FindBugs, and PMD.


Format source code in Eclipse and manage import statements
Eclipse provides the ability to automatically format source code and manage import statements (and Remove unused statements ). You can use the following shortcut keys to use these functions.
Ctrl + Shift + F-format the source code.
Ctrl + Shift + O-manage import statements and Remove unused statements.
In addition to manually executing these two functions, you can also enable Eclipse to automatically format the source code and manage the import statements when saving files. In Eclipse, go to Window-> Preferences-> Java-> Editor-> Save Actions and enable Perform the selected actions on save, and select Format source code and organize imports.


1. Avoid Multiple return statements (exit points) in the method ):
In your method, make sure there is only one exit point. Do not use more than one return statement in a method.


For example, the following code is not recommended because it has multiple exit points (return statements ). 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 written in this way (of course, the following code can be improved and I will talk about it later ). 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}


2. Simplify the if-else method:
We wrote some tool methods that only require one parameter, checked some conditions, and returned a value according to the conditions. For example, the isEligible method shown 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}


You can use only one return statement to override this method.
1 private boolean isEligible (int age ){

2 return age> 18;

3}


3. Do not create a new instance for Boolean, Integer, or String:
Avoid creating new instances such as Boolean, Integer, and String. Use Boolean. valueOf (true) instead of new Boolean (true ). The two methods have similar effects but can improve the performance.


4. Use braces around the code block:
Never forget to use braces around block-type statements (such as if, for, and while. This reduces code ambiguity and avoids new bugs when you modify code blocks.
1 if (age> 18) is not recommended)

2 return true;

3 else

4 return false;


1 if (age> 18 ){

2 return true;

3} else {

4 return false;

5}


5. Declare the parameters of the method as final:
The parameter of the method is always declared as final in all compatible places. In this way, when you accidentally modify the parameter value, the compilation will receive a warning and the byte code generated by compilation will be optimized.
1 private boolean isEligible (final int age) {...} is recommended ){...}


6. Name the public static final member variable in uppercase:
Public static final type variables are always named in uppercase. This makes it easy to distinguish between constants and local variables.
Not recommended
1 public static final String testAccountNo = "12345678 ";


1 public static final String TEST_ACCOUNT_NO = "12345678 ";


7. Merge multiple if statements into one:
The following code 1 if (age> 18 ){

2 if (voted = false ){

3 // eligible to vote.

4}

5}


You can use an if statement to rewrite it to: 1 if (age> 18 &&! Voted ){

2 // eligible to vote

3}


8. Do not forget to add the default statement to the switch:
Always add a default statement to the switch.


9. To avoid repeated use of the same string, create a constant:
If you need to use the same string in multiple places, create a String constant.
The following code: view source print? 1 private void someMethod (){

2 logger. log ("My Application" + e );

3 ....

4 ....

5 logger. log ("My Application" + f );

6}


You can create a constant to replace the string "My Application ":
1 public static final String MY_APP = "My Application ";

2

3 private void someMethod (){

4 logger. log (MY_APP + e );

5 ....

6 ....

7 logger. log (MY_APP + f );

8}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.