These rules deal with different optimizations that generally apply to performance best practices.
A local variable assigned only once can be declared final.
This rule is defined by the following Java class: net.sourceforge.pmd.rules.optimization.LocalVariableCouldBeFinal
Here's an example of code that would trigger this rule:
public void foo () { String a = "a"; //if a will not be assigned again it is better to do this: final String b = "b"; ... }
A method argument that is never assigned can be declared final.
This rule is defined by the following Java class: net.sourceforge.pmd.rules.optimization.MethodArgumentCouldBeFinal
Here's an example of code that would trigger this rule:
public void foo (String param) { // do stuff with param never assigning it // better: public void foo (final String param) { }
Detects when a new object is created inside a loop
This rule is defined by the following Java class: net.sourceforge.pmd.rules.optimization.AvoidInstantiatingObjectsInLoops
Here's an example of code that would trigger this rule:
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 } } }
ArrayList is a much better Collection implementation than Vector.
This rule is defined by the following XPath expression:
//AllocationExpression/ClassOrInterfaceType[@Image='Vector' or @Image='java.util.Vector']
Here's an example of code that would trigger this rule:
public class SimpleTest extends TestCase { public void testX() { Collection c = new Vector(); // This achieves the same with much better performance // Collection c = new ArrayList(); } }
Since it passes in a literal of length 1, this call to String.startsWith can be rewritten using String.charAt(0) to save some time.
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, '"')] ]
Here's an example of code that would trigger this rule:
public class Foo { boolean checkIt(String x) { return x.startsWith("a"); } }
Finds usages of += for appending strings.
This rule is defined by the following XPath expression:
//StatementExpression [PrimaryExpression/PrimaryPrefix/Name [@Image = ancestor::MethodDeclaration//LocalVariableDeclaration [./Type//ClassOrInterfaceType[@Image = 'String']]/VariableDeclarator/VariableDeclaratorId/@Image]] //AssignmentOperator[@Compound='true']
Here's an example of code that would trigger this rule:
String a; a = "foo"; a += " bar"; // better would be: StringBuffer a = new StringBuffer("foo"); a.append(" bar);