1. Format source code
Ctrl + Shift + F – Format the source code.
Ctrl + SHIFT + o– Manage import statements and remove unused statements
In addition to performing these two functions manually, you can have eclipse automatically format the source code and automatically manage the import statement when saving the file. To do this, in Eclipse, go to Window-> Preferences-> Java-> Editor-> Save actions and enable perform the selected actions on S Ave, select Format Source code and organize imports.
2. Avoid multiple return statements (exit points) in the method
The following code is not recommended because it has more than one exit point (return statement):
Private Boolean iseligible (int age) {
if [age >] return
true;
} else{return
false;
}
The above code can be written like this:
Private Boolean iseligible (int age) {
Boolean result = false;
if (age > A) result
= true;
}
return result;
}
It can also be written as:
Private Boolean iseligible (int age) {return
age>18;
}
3. Do not create a new instance for Boolean,integer or string
Avoid creating new instances such as boolean,integer,string. Use Boolean.valueof (true) instead of new Boolean (true). The two kinds of writing effect are similar but can improve performance.
4. Name the public static final type member variable in uppercase:
5. If you need to use the same string in more than one place, create a string constant to use.