摘抄自:http://pmd.sourceforge.net/rules/optimizations.html
一、Optimization Rules:最佳规则
1、LocalVariableCouldBeFinal:A local variable assigned only once can be declared final.
2、MethodArgumentCouldBeFinal:A method argument that is never assigned can be declared final.
3、AvoidInstantiatingObjectsInLoops:Detects when a new object is created inside a loop
public class Something {
public static void main( String as[] ) {
for (int i = 0; i < 10; i++) {
Foo f = new Foo(); //Avoid this whenever you can it's really expensive
}
}
}
5、UseArrayListInsteadOfVector:ArrayList is a much better Collection implementation than Vector.
6、UseArraysAsList:The java.util.Arrays class has a "asList" method that should be used when you want to create a new List from an array of objects. It is faster than executing a loop to copy all the elements of the array one by one.
public void foo(Integer[] ints) {
// could just use Arrays.asList(ints)
List l= new ArrayList(10);
for (int i=0; i< 100; i++) {
l.add(ints[i]);
}
for (int i=0; i< 100; i++) {
l.add(a[i].toString()); // won't trigger the rule
}
}
7、AvoidArrayLoops:Instead of copying data between two arrays, use System.arraycopy method.
public void bar() {
int[] a = new int[10];
int[] b = new int[10];
for (int i=0;i<10;i++) {
b[i]=a[i];
}
}
// this will trigger the rule
for (int i=0;i<10;i++) {
b[i]=a[c[i]];
}
}
8 、AddEmptyStirng:Finds empty string literals which are being added. This is an inefficient(无效) way to convert any type to a String.
String s = "" + 123; // bad
String t = Integer.toString(456); // ok
public void doSomething() {
try {
FileInputStream fis = new FileInputStream("/tmp/bugger");
} catch (IOException ioe) {
// not good
}
for (int i = 0; i < 10; i++) {
for (int k = 0; k < 20; i++) {
System.out.println("Hello");
}
}
// this wastes an object
String foo = new Integer(x).toString();
// this is better
return Integer.toString(x);
Object baz;
Object bar() {
if(baz == null) { //baz may be non-null yet not fully created
synchronized(this){
if(baz == null){
baz = new Object();
}
}
}
return baz;
}
public void bar() {
int x = 42;
return;
}
public final class Foo {
// This final modifier is not necessary, since the class is final
// and thus, all methods are final
private final void foo() {
}
}
void bar() {
if (x) {
if (y) {
// do stuff
}
}
}
public static void main(String[] args) {
Collection c=new ArrayList();
Integer obj=new Integer(1);
c.add(obj);
// this would trigger the rule (and throw a ClassCastException if executed)
Integer[] a=(Integer [])c.toArray();
// this wouldn't trigger the rule
Integer[] b=(Integer [])c.toArray(new Integer[c.size()]);
}
// should be &&
if (string!=null || !string.equals(""))
return string;
// should be ||
if (string==null && string.equals(""))
return string;
13、AvoidUsingOctalValue:
Integer literals should not start with zero. Zero means that the rest of literal will be interpreted(理解) as an octal value(八进制).
public class Foo {
int i = 012; // set i with 10 not 12
int j = 010; // set j with 8 not 10
k = i * j; // set k with 80 not 120
}