Today again want to start Java Code Static Detection Tool utilization problem, mainly try to use PMD again, found a lot of code coding problems and good code recommendations, and learned a lot of their own prior to the Java convenient basic knowledge, feel very good, the relevant understanding of the good rules of the recollection summary, In the future coding process to be ready to use the Code static Check tool, with good code static check tool habits. The PMD check rules used today are as follows:
The IF expression must use {}, regardless of how many statements the while loop must use {}, regardless of how many statements the IfElse expression must use {} for The Loop statement must use {} If the method returns a Boolean, then avoid unnecessary if: Then.. Else statement to prevent if statements are nested too deep (resulting in reduced code readability) Workaround: It is recommended that if nesting does not exceed 2 layers. Use the tool method to encapsulate more if statements or to place nested if expressions in the same hierarchy. When ignoring case for string comparisons, use String.equalsignorecase, and do not use String.tolowercase. The former has better performance and avoids the localization problems caused by the latter to avoid method-level synchronization Block-level synchronization ensures that the code that really needs to be synchronized is included. The IsEmpty method of the java.util.collection class using the IsEmpty method of the collection class provides a way to determine whether a collection class contains elements. Do not use the size () and 0 comparisons to repeat the methods already provided by the class library. This principle tells us a universal principle: reuse. Reuse as far as possible, make full use of existing resources, do not repeat yourself (DRY). For example, Apache provides a lot of tools for us to use, we do not need to write it ourselves. The code that is not used is a rule: remove it without using it, keep the code clean and tidy. No use of the code includes: Ø No use of the Private member O no use of the local variable ø No use of the Private method o No method parameters (parameters defined, but the method is not used in this parameter) when building StringBuffer or StringBuilder, if you know the length, specify , so the performance is better not specified, the default length is 16, so when the length is not enough, there will be a scaling action. If the local variable is only assigned one time, declare it as final if the method parameter is never re-assigned, then declare it as final if you want to build a list from an array, Use arrays.aslist array replication, use System.arraycopy, do not convert numbers to string using the Loop "" +123, not efficient example code: String s = "" + 123; bad String t = integer.tostring (456); ok avoid a variety of "empty" statements in the code empty catch, empty if, empty WHile, empty try, empty Finally, null switch, empty synchronized block, empty static block
PMD for static detection of Java code