PMD 规则集——Optimization Rules

Optimization Rules
优化规则

These rules deal with different optimizations that generally apply to performance best practices.

LocalVariableCouldBeFinal

Since: PMD 2.2

局部变量只被赋值一次可以声明为final.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.optimization.LocalVariableCouldBeFinal

例子:

public class Bar {
 public void foo() {
  String a = "a"; // if a will not be assigned again it is better to do this:
  final String b = "b";
 }
}         

MethodArgumentCouldBeFinal

Since: PMD 2.2

从来没有赋过值的方法参数可以声明为final.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.optimization.MethodArgumentCouldBeFinal

Example: 
  

public void foo (String param) {
  // do stuff with param never assigning it
  // better: public void foo (final String param) {
}

           

AvoidInstantiatingObjectsInLoops

Since: PMD 2.2

侦测在循环中创建一个新对象

This rule is defined by the following Java class: net.sourceforge.pmd.rules.optimization.AvoidInstantiatingObjectsInLoops

Example:                

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
    }
  }
}

    
           

UseArrayListInsteadOfVector

Since: PMD 3.0

ArrayList是个比Vector更好的Collection实现。

This rule is defined by the following XPath expression:

//AllocationExpression
 /ClassOrInterfaceType[@Image='Vector' or @Image='java.util.Vector']

             

例子:                

public class SimpleTest extends TestCase {
 public void testX() {
  Collection c = new Vector();
  // This achieves the same with much better performance
  // Collection c = new ArrayList();
 }
}          

SimplifyStartsWith

Since: PMD 3.1

由于传入长度为1的字面意义参数,这调用String.startsWith 可以使用 String.charAt(0) 重写节省一些时间。

This rule is defined by the following XPath expression:

//PrimaryExpression
 [PrimaryPrefix/Name
  [ends-with(@Image, '.startsWith')]]
 [PrimarySuffix/Arguments/ArgumentList
  /Expression/PrimaryExpression/PrimaryPrefix
  /Literal
   [string-length(@Image)=3]
   [starts-with(@Image, '"')]
   [ends-with(@Image, '"')]
 ]

  
           

例子:            
  

public class Foo {
  boolean checkIt(String x) {
      return x.startsWith("a");
  }
}

           

UseStringBufferForStringAppends

Since: PMD 3.1

查找使用+= 追加字符串。

This rule is defined by the following Java class: net.sourceforge.pmd.rules.optimization.UseStringBufferForStringAppends

例子:                
      
      

public class Foo {
 void bar() {
  String a;
  a = "foo";
  a += " bar";
  // better would be:
  // StringBuffer a = new StringBuffer("foo");
  // a.append(" bar);
 }
}

 
           
           

UseArraysAsList

Since: PMD 3.5

 当你想要从一个对象数组创建一个新List ,应该使用java.util.Arrays 类的"asList"方法. 比执行一个循环逐个地复制数组的所有元素快地多

This rule is defined by the following XPath expression:   
   

//Statement[
    (ForStatement) and (count(.//IfStatement)=0)
   ]
   //StatementExpression[
    PrimaryExpression/PrimaryPrefix/Name[
	 substring-before(@Image,'.add') = ancestor::MethodDeclaration//LocalVariableDeclaration[
      ./Type//ClassOrInterfaceType[
       @Image = 'Collection' or 
       @Image = 'List' or @Image='ArrayList'
      ]
     ]
     /VariableDeclarator/VariableDeclaratorId[
      count(..//AllocationExpression/ClassOrInterfaceType[
       @Image="ArrayList"
      ]
      )=1
     ]/@Image
    ]
   and
   PrimaryExpression/PrimarySuffix/Arguments/ArgumentList/Expression/PrimaryExpression/PrimaryPrefix/Name
   [@Image = ancestor::MethodDeclaration//LocalVariableDeclaration
   [@Array="true"]/VariableDeclarator/VariableDeclaratorId/@Image]
   /../..[count(.//PrimarySuffix)
   =1]/PrimarySuffix/Expression/PrimaryExpression/PrimaryPrefix
   /Name
   ]

例子:

public class Test {
    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
     }
    }
   }

 
     
           

AvoidArrayLoops

Since: PMD 3.5

替换两个数组之间复制数据, 使用System.arraycopy方法

This rule is defined by the following XPath expression:        

//Statement[(ForStatement or WhileStatement) and
count(*//AssignmentOperator[@Image = '='])=1
and
*/Statement
[
./Block/BlockStatement/Statement/StatementExpression/PrimaryExpression
/PrimaryPrefix/Name/../../PrimarySuffix/Expression
[(PrimaryExpression or AdditiveExpression) and count
(.//PrimaryPrefix/Name)=1]//PrimaryPrefix/Name/@Image
and
./Block/BlockStatement/Statement/StatementExpression/Expression/PrimaryExpression
/PrimaryPrefix/Name/../../PrimarySuffix[count
(..//PrimarySuffix)=1]/Expression[(PrimaryExpression
or AdditiveExpression) and count(.//PrimaryPrefix/Name)=1]
//PrimaryPrefix/Name/@Image
]]

 

例子:                
 

public class Test {
 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]];
            }

        }
    }

      
           

UnnecessaryWrapperObjectCreation

Since: PMD 3.8

Parsing method 应该直接地调用Parsing方法。

This rule is defined by the following Java class: net.sourceforge.pmd.rules.optimization.UnnecessaryWrapperObjectCreation

例子:                

public int convert(String s) {
  int i, i2;

  i = Integer.valueOf(s).intValue(); // this wastes an object
  i = Integer.parseInt(s); // this is better

  i2 = Integer.valueOf(i).intValue(); // this wastes an object
  i2 = i; // this is better

  return i2;
}

           

AddEmptyString

Since: PMD 4.0

查找添加的空白字符串. 这是一个低效的方法,将任何类型转换为字符串。

This rule is defined by the following XPath expression:                                     

//AdditiveExpression/PrimaryExpression/PrimaryPrefix/Literal[@Image='""']

 

例子:

String s = "" + 123; // bad 
String t = Integer.toString(456); // ok 

                
             
        
            
        
           

你可能感兴趣的:(C++,c,.net,C#,performance)