Starting with static code analysis tools, we can say a few things that everyone is familiar with, such as PMD, Checkstyle, and Findbug. Yes, these are good tools used to improve the quality of our code during our daily coding code. This article will start with the rules implemented by the PMD tool and learn the code optimization skills through the definition of these rules, we hope that the introduction of these skills will help you improve the quality of coding. It is also a good way to learn how to improve the quality of code writing.
The following are some code optimization suggestions summarized from the PMD rules:
1. The local variable is assigned only once and can be declared as final.
Note: after the final mark is used, Java will automatically replace the changes with constants during compilation, so that the program running efficiency will be improved.
2. If you pass in a literal parameter with a length of 1, you can use the String. charAt (0) method to save the running time by calling String. startsWith.
Note: This is easy to understand. The String object itself is a char array. The internal processing of the String. startsWith method is complicated, and there are loop operations, so the efficiency is far less than charAt operations.
"Abc". startsWith ("A") // bad
"Abc". charAt (0) = 'A' // good
3. concatenate strings and use StringBuffer instead of the "+" operator.
Note: Everyone knows this. Because the String object is spliced directly, A New String object is generated for storage. StringBuffer applies for a large memory space (char array). For splicing, if the size of the char array does not exceed the size of the array, it is directly appended to the end of the array. note that the default size of StringBuffer is 16 characters, so we recommend that you specify the size of the created array when using it.
4. String indexOf method. If the parameter is a single letter, char is used.
Abc ". indexOf (" A ") // bad
Abc ". indexOf ('A') // good
Note: The String indexOf method uses two methods for String and char search. If you are interested, you can check the original code of String. The String search method is more complex than the char search method, relative time consumption
5. Do not use the StringBuffer constructor without parameters (the initial length is 16 characters)
Note: You may have some misunderstandings about StringBuffer and think that the initial space of StringBuffer is relatively large, but in fact it creates an array of only 16 characters by default. Once it exceeds the limit, you need to apply for a new array space, which will incur additional overhead.
6. Non-thread-safe Singleton NonThreadSafeSingleton
The following code is a common implementation method of Singleton.
However, a problem introduced by this Code is that it runs in the multi-threaded mode and may cause the re-creation of objects. Solution: add the synchronized keyword to the getFoo method.
7. The hashCode and equals methods must be overwritten at the same time.
Note: This is also a problem that people occasionally ignore. HashCode and equlas are mainly used in collection classes of basic Hash algorithms, such as HashMap.
In the implementation of HashMap, the logic for determining whether the other party is equal is as follows:
That is to say, in HashMap, if two objects are equal, they must be equivalent to hashCode and the equals method also returns equal.
If only equals is rewritten in an object, but hashCode is not rewritten, the "repeated" object will also occur in the HashSet.
The following is a problematic sample code:
8. When the BigDecimal object is initialized, the String parameter is used for the floating point data value (for precision problems)
Note: For example, the true value of the new BigDecimal (0.1) value may be. 1000000000000000055511151231257827021181583404541015625.
Good Luck!
Yours Matthew!