Recently, I did a code cleanup for the Java project. After cleansing, I found that a common set of code violations (that is, code that does not represent code errors) repeats itself in the code. Therefore, I summarize these common code violations into a list and share them with you to help Java enthusiasts improve the quality and maintainability of their codes.
This list is not based on any rules or order, all of which are checked out through the code quality tools including Checkstyle,findbugs and PMD. Look together:
First, the Eclipse compiler provides source code format input
Eclipse provides automatic source formatting options and organizes input (removing unused code). You can use these shortcut keys to do this.
Ctrl + Shift + F-source code format
Ctrl + Shift + o-- organization input and delete unused code
Instead of calling these two functions manually, you can save the file at any time, depending on the Eclipse auto-format and auto-organize options.
Operation Steps, in Eclipse, enter the save actions, Editor, Java, Window, Preferences, and save in the selected way, and finally check the Format Source code + Organize imports.
Second, avoid multiple returns (exit points)
According to your method, make sure there is only one exit point. Do not use returns in the same place or in multiple places. For example, the following code, notRECOMMENDED, is not recommended because there are multiple exit points (return statements).
- Private Boolean iseligible (int age) {
- if ( age > ) {
- return true;
- }else{
- return false;
- }
- }
The following code has been improved, which is later version.
- Private Boolean iseligible (int age) {
- Boolean result;
- if ( age > ) {
- result = true;
- }else{
- result = false;
- }
- return result;
- }
Iii. simplification of If-else
I wrote a few useful methods for reference, checking the statement condition and returning the value based on that condition. For example, consider the Iseligible method, as you have seen before:
- Private Boolean iseligible (int age) {
- Boolean result;
- if ( age > ) {
- result = true;
- }else{
- result = false;
- }
- return result;
- }
The entire method is rewritten with a single return statement:
- Private Boolean iseligible (int age) {
- return Age > 18;
- }
Iv. do not create a new instance of Boolean, Integer, or string
Avoid creating new instances for boolean,integer,string. For example, using the new Boolean (true), boolean,valueof (true). The modified statement is basically the same as the previous effect, except for improved performance.
V. Using BRACES module statements
Never forget to use braces module statements such as if, for, and while. The advantage of this is that when you modify a module-level statement, you reduce the blurring of the code and avoid the opportunity to introduce bugs.
Not recommended:
- if ( age > )
- result = true;
- Else
- result = false;
Suggestions:
- if ( age > ) {
- result = true;
- }else{
- result = false;
- }
Vi. marking method parameters in final type, applicable at all times
Keep in mind that the method parameters are marked with the final type and are applicable at all times. The advantage of this is that when you accidentally modify a parameter value, the compiler warns you, and it also optimizes compiler code bytes in a better way.
Suggestions:
- Private Boolean iseligible (final int age) {...}
Vii. name public static final field in uppercase
The public static final field is named in uppercase (often referred to as a constant). This allows you to easily distinguish between a constant field and a local variable.
Not recommended:
- Public static Final String Testaccountno = "12345678" ;
Suggestions:
- Public static Final String Test_account_no = "12345678" ;,
Eight, combined into a single if statement
In as many cases as possible, combine multiple if statements into a single if statement, such as the following code:
- if ( age > ) {
- if ( voted = = False) {
- Eligible to vote.
- }
- }
Merge into a single if statement:
- if ( age > &&!voted) {
- Eligible to vote
- }
Nine, switch should have default
Always add default to the switch statement.
X. Use constants to avoid repeating the same string values
If you must use strings in multiple places, use constants to avoid repeating a string that has the same value.
For example, look at the following code:
- private void SomeMethod () {
- Logger.log ("My application" + E);
- ....
- ....
- Logger.log ("My application" + f);
- }
string literal "My app" can be a constant and can be used in code.
- public static final string span class= "attribute" >my_app = "my Application " ;
-
- private void somemethod () {
- logger.log (my_app + e);
- ...
- ....
- logger.log (my_app + f);
- }