##### For example

- Run PMD on a Sourceforge project

## Design Rules

The Design Ruleset contains a collection of rules that find questionable designs.

### UseSingleton

If you have a class that has nothing but static methods, consider making it a Singleton. Note that this doesn't apply to abstract classes, since their subclasses may well include non-static methods. Also, if you want this class to be a Singleton, remember to add a private constructor to prevent instantiation.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.design.UseSingleton

Here's an example of code that would trigger this rule:

    public class MaybeASingleton {
     public static void foo() {}
     public static void bar() {}
    }

### SimplifyBooleanReturns

Avoid unnecessary if..then..else statements when returning a boolean

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

Here's an example of code that would trigger this rule:

    public class Foo {
      private int bar =2;
      public boolean isBarEqualsTo(int x) {
        // this bit of code
        if (bar == x) {
         return true;
        } else {
         return false;
        }
        // can be replaced with a simple
        // return bar == x;
      }
    }

### SimplifyBooleanExpressions

Avoid unnecessary comparisons in boolean expressions - this makes simple code seem complicated.

This rule is defined by the following XPath expression:

    //EqualityExpression/PrimaryExpression
     /PrimaryPrefix/Literal/BooleanLiteral

Here's an example of code that would trigger this rule:

    public class Bar {
     // can be simplified to
     // bar = isFoo();
     private boolean bar = (isFoo() == true);

     public isFoo() { return false;}
    }

### SwitchStmtsShouldHaveDefault

Switch statements should have a default label.

This rule is defined by the following XPath expression:

    //SwitchStatement[not(SwitchLabel[@Default='true'])]

Here's an example of code that would trigger this rule:

    public class Foo {
     public void bar() {
      int x = 2;
      switch (x) {
       case 2: int j = 8;
      }
     }
    }

### AvoidDeeplyNestedIfStmts

Deeply nested if..then statements are hard to read.

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

Here's an example of code that would trigger this rule:

    public class Foo {
     public void bar(int x, int y, int z) {
      if (x&gt;y) {
       if (y&gt;z) {
        if (z==x) {
         // whew, too deep
        }
       }
      }
     }
    }

This rule has the following properties:

Name | Default value | Description
--- | --- | ---
problemDepth | 3 | The if statement depth reporting threshold

### AvoidReassigningParameters

Reassigning values to parameters is a questionable practice. Use a temporary local variable instead.

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

Here's an example of code that would trigger this rule:

    public class Foo {
     private void foo(String bar) {
      bar = "something else";
     }
    }

### SwitchDensity

A high ratio of statements to labels in a switch statement implies that the switch statement is doing too much work. Consider moving the statements either into new methods, or creating subclasses based on the switch variable.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.design.SwitchDensityRule

Here's an example of code that would trigger this rule:

    public class Foo {
     public void bar(int x) {
       switch (x) {
         case 1: {
           // lots of statements
           break;
         } case 2: {
           // lots of statements
           break;
         }
       }
     }
    }

This rule has the following properties:

Name | Default value | Description
--- | --- | ---
minimum | 10 | The switch statement ratio reporting threshold

### ConstructorCallsOverridableMethod

Calling overridable methods during construction poses a risk of invoking methods on an incompletely constructed object. This situation can be difficult to discern. It may leave the sub-class unable to construct its superclass or forced to replicate the construction process completely within itself, losing the ability to call super(). If the default constructor contains a call to an overridable method, the subclass may be completely uninstantiable. Note that this includes method calls throughout the control flow graph - i.e., if a constructor Foo() calls a private method bar() that calls a public method buz(), there's a problem.

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

Here's an example of code that would trigger this rule:

    public class SeniorClass {
      public SeniorClass(){
          toString(); //may throw NullPointerException if overridden
      }
      public String toString(){
        return "IAmSeniorClass";
      }
    }
    public class JuniorClass extends SeniorClass {
      private String name;
      public JuniorClass(){
        super(); //Automatic call leads to NullPointerException
        name = "JuniorClass";
      }
      public String toString(){
        return name.toUpperCase();
      }
    }

### AccessorClassGeneration

Instantiation by way of private constructors from outside of the constructor's class often causes the generation of an accessor. A factory method, or non-privitization of the constructor can eliminate this situation. The generated class file is actually an interface. It gives the accessing class the ability to invoke a new hidden package scope constructor that takes the interface as a supplementary parameter. This turns a private constructor effectively into one with package scope, though not visible to the naked eye.

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

Here's an example of code that would trigger this rule:

    public class Outer {
     void method(){
      Inner ic = new Inner();//Causes generation of accessor class
     }
     public class Inner {
      private Inner(){}
     }
    }

### FinalFieldCouldBeStatic

If a final field is assigned to a compile-time constant, it could be made static, thus saving overhead in each object

This rule is defined by the following XPath expression:

    //FieldDeclaration
     [@Final='true' and @Static='false']
     [not (../../../../ClassOrInterfaceDeclaration[@Interface='true'])]
       /VariableDeclarator/VariableInitializer/Expression
        /PrimaryExpression/PrimaryPrefix/Literal

Here's an example of code that would trigger this rule:

    public class Foo {
     public final int BAR = 42; // this could be static and save some space
    }

### CloseResource

Ensure that resources (like Connection, Statement, and ResultSet objects) are always closed after use

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

Here's an example of code that would trigger this rule:

    public class Bar {
     public void foo() {
      Connection c = pool.getConnection();
      try {
        // do stuff
      } catch (SQLException ex) {
        // handle exception
      } finally {
        // oops, should close the connection using 'close'!
        // c.close();
      }
     }
    }

This rule has the following properties:

Name | Default value | Description
--- | --- | ---
types | Connection,Statement,ResultSet |

### NonStaticInitializer

A nonstatic initializer block will be called any time a constructor is invoked (just prior to invoking the constructor). While this is a valid language construct, it is rarely used and is confusing.

This rule is defined by the following XPath expression:

    //ClassOrInterfaceBodyDeclaration/Initializer[@Static='false']

Here's an example of code that would trigger this rule:

    public class MyClass {
     // this block gets run before any call to a constructor
     {
      System.out.println("I am about to construct myself");
     }
    }

### DefaultLabelNotLastInSwitchStmt

By convention, the default label should be the last label in a switch statement.

This rule is defined by the following XPath expression:

    //SwitchStatement
     [not(SwitchLabel[position() = last()][@Default='true'])]
     [SwitchLabel[@Default='true']]

Here's an example of code that would trigger this rule:

    public class Foo {
     void bar(int a) {
      switch (a) {
       case 1:  // do something
          break;
       default:  // the default case should be last, by convention
          break;
       case 2:
          break;
      }
     }
    }

### NonCaseLabelInSwitchStatement

A non-case label (e.g. a named break/continue label) was present in a switch statement. This legal, but confusing. It is easy to mix up the case labels and the non-case labels.

This rule is defined by the following XPath expression:

    //SwitchStatement//BlockStatement/Statement/LabeledStatement

Here's an example of code that would trigger this rule:

    public class Foo {
     void bar(int a) {
      switch (a) {
       case 1:
          // do something
          break;
       mylabel: // this is legal, but confusing!
          break;
       default:
          break;
      }
     }
    }

### OptimizableToArrayCall

A call to Collection.toArray can use the Collection's size vs an empty Array of the desired type.

This rule is defined by the following XPath expression:

    //PrimaryExpression
    [PrimaryPrefix/Name[ends-with(@Image, 'toArray')]]
    [
    PrimarySuffix/Arguments/ArgumentList/Expression
     /PrimaryExpression/PrimaryPrefix/AllocationExpression
     /ArrayDimsAndInits/Expression/PrimaryExpression/PrimaryPrefix/Literal[@Image='0']
    ]

Here's an example of code that would trigger this rule:

    class Foo {
     void bar(Collection x) {
       // A bit inefficient
       x.toArray(new Foo[0]);
       // Much better; this one sizes the destination array, avoiding
       // a reflection call in some Collection implementations
       x.toArray(new Foo[x.size()]);
     }
    }

### BadComparison

Avoid equality comparisons with Double.NaN - these are likely to be logic errors.

This rule is defined by the following XPath expression:

    //EqualityExpression[@Image='==']
     /PrimaryExpression/PrimaryPrefix
     /Name[@Image='Double.NaN' or @Image='Float.NaN']

Here's an example of code that would trigger this rule:

    public class Bar {
     boolean x = (y == Double.NaN);
    }

### EqualsNull

Newbie programmers sometimes get the comparison concepts confused and use equals() to compare to null.

This rule is defined by the following XPath expression:

    //PrimaryExpression
     [
    PrimaryPrefix/Name[ends-with(@Image, 'equals')]
    or
    PrimarySuffix[ends-with(@Image, 'equals')]
    ]
    [PrimarySuffix/Arguments/ArgumentList
      /Expression/PrimaryExpression/PrimaryPrefix
       /Literal/NullLiteral]

Here's an example of code that would trigger this rule:

    class Bar {
       void foo() {
           String x = "foo";
           if (x.equals(null)) { // bad!
            doSomething();
           }
       }
    }

### ConfusingTernary

In an "if" expression with an "else" clause, avoid negation in the test. For example, rephrase: if (x != y) diff(); else same(); as: if (x == y) same(); else diff(); Most "if (x != y)" cases without an "else" are often return cases, so consistent use of this rule makes the code easier to read. Also, this resolves trivial ordering problems, such as "does the error case go first?" or "does the common case go first?".

This rule is defined by the following Java class: net.sourceforge.pmd.rules.design.ConfusingTernary

Here's an example of code that would trigger this rule:

    public class Foo {
     boolean bar(int x, int y) {
      return (x != y) ? diff : same;
     }
    }

### InstantiationToGetClass

Avoid instantiating an object just to call getClass() on it; use the .class public member instead

This rule is defined by the following XPath expression:

    //PrimarySuffix
     [@Image='getClass']
     [parent::PrimaryExpression
      [PrimaryPrefix/AllocationExpression]
      [count(PrimarySuffix) = 2]
     ]

Here's an example of code that would trigger this rule:

    public class Foo {
     // Replace this
     Class c = new String().getClass();
     // with this:
     Class c = String.class;
    }

### IdempotentOperations

Avoid idempotent operations - they are silly.

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

Here's an example of code that would trigger this rule:

    public class Foo {
     public void bar() {
      int x = 2;
      x = x;
     }
    }

### SimpleDateFormatNeedsLocale

Be sure to specify a Locale when creating a new instance of SimpleDateFormat.

This rule is defined by the following XPath expression:

    //AllocationExpression
     [ClassOrInterfaceType[@Image='SimpleDateFormat']]
     [Arguments[@ArgumentCount=1]]

Here's an example of code that would trigger this rule:

    public class Foo {
     // Should specify Locale.US (or whatever)
     private SimpleDateFormat sdf = new SimpleDateFormat("pattern");
    }

### ImmutableField

Identifies private fields whose values never change once they are initialized either in the declaration of the field or by a constructor. This aids in converting existing classes to immutable classes.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.design.ImmutableField

Here's an example of code that would trigger this rule:

    public class Foo {
      private int x; // could be final
      public Foo() {
          x = 7;
      }
      public void foo() {
         int a = x + 2;
      }
    }

### UseLocaleWithCaseConversions

When doing a String.toLowerCase()/toUpperCase() call, use a Locale. This avoids problems with certain locales, i.e. Turkish.

This rule is defined by the following XPath expression:

    //PrimaryExpression
    [PrimaryPrefix/Name
     [ends-with(@Image, 'toLowerCase') or ends-with(@Image,
    'toUpperCase')]
     ]
    [PrimarySuffix/Arguments[@ArgumentCount=0]]

Here's an example of code that would trigger this rule:

    class Foo {
     // BAD
     if (x.toLowerCase().equals("list"))...
     /*
     This will not match "LIST" when in Turkish locale
     The above could be
     if (x.toLowerCase(Locale.US).equals("list")) ...
     or simply
     if (x.equalsIgnoreCase("list")) ...
     */
     // GOOD
     String z = a.toLowerCase(Locale.EN);
    }

### AvoidProtectedFieldInFinalClass

Do not use protected fields in final classes since they cannot be subclassed. Clarify your intent by using private or package access modifiers instead.

This rule is defined by the following XPath expression:

    //ClassOrInterfaceDeclaration
     [@Final='true']//FieldDeclaration[@Protected='true']

Here's an example of code that would trigger this rule:

    public final class Bar {
     private int x;
     protected int y;  // &lt;-- Bar cannot be subclassed, so is y really private or package visible???
     Bar() {}
    }

### AssignmentToNonFinalStatic

Identifies a possible unsafe usage of a static field.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.design.AssignmentToNonFinalStatic

Here's an example of code that would trigger this rule:

    public class StaticField {
       static int x;
       public FinalFields(int y) {
        x = y; // unsafe
       }
    }

### MissingStaticMethodInNonInstantiatableClass

A class that has private constructors and does not have any static methods or fields cannot be used.

This rule is defined by the following XPath expression:

    //ClassOrInterfaceDeclaration[@Nested='false'][
    ( count(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration)&gt;0
      and count(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration) = count(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration[@Private='true']) )
    and
    count(.//MethodDeclaration[@Static='true'])=0
    and
    count(.//FieldDeclaration[@Public='true'][@Static='true'])=0
    ]

Here's an example of code that would trigger this rule:

    /* This class is unusable, since it cannot be
     instantiated (private constructor),
     and no static method can be called.
     */
    public class Foo {
     private Foo() {}
     void foo() {}
    }

### AvoidSynchronizedAtMethodLevel

Method level synchronization can backfire when new code is added to the method. Block-level synchronization helps to ensure that only the code that needs synchronization gets it.

This rule is defined by the following XPath expression:

    //MethodDeclaration[@Synchronized='true']

Here's an example of code that would trigger this rule:

    public class Foo {
     // Try to avoid this
     synchronized void foo() {
     }
     // Prefer this:
     void bar() {
      synchronized(this) {
      }
     }
    }

### MissingBreakInSwitch

A switch statement without an enclosed break statement may be a bug.

This rule is defined by the following XPath expression:

    //SwitchStatement
    [count(.//BreakStatement)=0]
    [count(SwitchLabel) &gt; 0]
    [count(BlockStatement/Statement/ReturnStatement) &lt; count (SwitchLabel)]

Here's an example of code that would trigger this rule:

    public class Foo {
     public void bar(int status) {
      switch(status) {
       case CANCELLED:
        doCancelled();
        // break; hm, should this be commented out?
       case NEW:
        doNew();
       case REMOVED:
        doRemoved();
       }
     }
    }

### UseNotifyAllInsteadOfNotify

Thread.notify() awakens a thread monitoring the object. If more than one thread is monitoring, then only one is chosen. The thread chosen is arbitrary; thus it's usually safer to call notifyAll() instead.

This rule is defined by the following XPath expression:

    //Statement/StatementExpression/PrimaryExpression
    [count(PrimarySuffix/Arguments/ArgumentList) = 0]
    [
    PrimaryPrefix[./Name[@Image='notify' or ends-with(@Image,'.notify')]
    or @Image='notify'
    or (./AllocationExpression and ../PrimarySuffix[@Image='notify'])
    ]
    ]

Here's an example of code that would trigger this rule:

    public class Foo {
     void bar() {
      x.notify();
      // If many threads are monitoring x, only one (and you won't know which) will be notified.
      // use instead:
      x.notifyAll();
     }
    }

### AvoidInstanceofChecksInCatchClause

Each caught exception type should be handled in its own catch clause.

This rule is defined by the following XPath expression:

    //CatchStatement/FormalParameter
     /following-sibling::Block//InstanceOfExpression/PrimaryExpression/PrimaryPrefix
      /Name[
       @Image = ./ancestor::Block/preceding-sibling::FormalParameter
        /VariableDeclaratorId/@Image
      ]

Here's an example of code that would trigger this rule:

    try { // Avoid this
     // do something
    } catch (Exception ee) {
     if (ee instanceof IOException) {
      cleanup();
     }
    }
    try {  // Prefer this:
     // do something
    } catch (IOException ee) {
     cleanup();
    }

### AbstractClassWithoutAbstractMethod

The abstract class does not contain any abstract methods. An abstract class suggests an incomplete implementation, which is to be completed by subclasses implementing the abstract methods. If the class is intended to be used as a base class only (not to be instantiated direcly) a protected constructor can be provided prevent direct instantiation.

This rule is defined by the following XPath expression:

    //ClassOrInterfaceDeclaration
     [@Abstract='true'
      and count( .//MethodDeclaration[@Abstract='true'] )=0 ]
      [count(ImplementsList)=0]

Here's an example of code that would trigger this rule:

    public abstract class Foo {
     void int method1() { ... }
     void int method2() { ... }
     // consider using abstract methods or removing
     // the abstract modifier and adding protected constructors
    }

### SimplifyConditional

No need to check for null before an instanceof; the instanceof keyword returns false when given a null argument.

This rule is defined by the following XPath expression:

    //Expression
     [ConditionalOrExpression
     [EqualityExpression[@Image='==']
      //NullLiteral
      and
      UnaryExpressionNotPlusMinus
       [@Image='!']//InstanceOfExpression[PrimaryExpression
         //Name/@Image = ancestor::ConditionalOrExpression/EqualityExpression
          //PrimaryPrefix/Name/@Image]]
    or
    ConditionalAndExpression
     [EqualityExpression[@Image='!=']//NullLiteral
     and
    InstanceOfExpression
     [PrimaryExpression[count(PrimarySuffix[@ArrayDereference='true'])=0]
      //Name/@Image = ancestor::ConditionalAndExpression
       /EqualityExpression//PrimaryPrefix/Name/@Image]]]

Here's an example of code that would trigger this rule:

    class Foo {
     void bar(Object x) {
      if (x != null &amp;&amp; x instanceof Bar) {
       // just drop the "x != null" check
      }
     }
    }

### CompareObjectsWithEquals

Use equals() to compare object references; avoid comparing them with ==.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.design.CompareObjectsWithEquals

Here's an example of code that would trigger this rule:

    class Foo {
     boolean bar(String a, String b) {
      return a == b;
     }
    }

### PositionLiteralsFirstInComparisons

Position literals first in String comparisons - that way if the String is null you won't get a NullPointerException, it'll just return false.

This rule is defined by the following XPath expression:

        //PrimaryExpression[PrimaryPrefix[Name[ends-with(@Image, '.equals')]][..//Literal]]
        [not(ancestor::Expression/ConditionalAndExpression//EqualityExpression[@Image='!=']//NullLiteral)]
        [not(ancestor::Expression/ConditionalOrExpression//EqualityExpression[@Image='==']//NullLiteral)]

Here's an example of code that would trigger this rule:

    class Foo {
     boolean bar(String x) {
      return x.equals("2"); // should be "2".equals(x)
     }
    }

### UnnecessaryLocalBeforeReturn

Avoid unnecessarily creating local variables

This rule is defined by the following Java class: net.sourceforge.pmd.rules.design.UnnecessaryLocalBeforeReturn

Here's an example of code that would trigger this rule:

      public class Foo {
        public int foo() {
          int x = doSomething();
          return x;  // instead, just 'return doSomething();'
        }
      }

### NonThreadSafeSingleton

Non-thread safe singletons can result in bad state changes. If possible, get rid of static singletons by directly instantiating the object. Static singletons are usually not needed as only a single instance exists anyway. Other possible fixes are to synchronize the entire method or to use an initialize-on-demand holder class (do not use the double-check idiom). See Effective Java, item 48.

This rule is defined by the following XPath expression:

    //MethodDeclaration[$checkNonStaticMethods='true' or @Static='true'][@Synchronized='false']
    //IfStatement[not (ancestor::SynchronizedStatement)]
     [Expression/EqualityExpression
       [PrimaryExpression/PrimaryPrefix/Literal/NullLiteral]
       [PrimaryExpression/PrimaryPrefix/Name[@Image=
           //FieldDeclaration[$checkNonStaticFields='true' or @Static='true']
           /VariableDeclarator/VariableDeclaratorId/@Image]]]
     [Statement//StatementExpression[AssignmentOperator]
           [PrimaryExpression/PrimaryPrefix/Name/@Image=
            ancestor::ClassOrInterfaceBody//FieldDeclaration[$checkNonStaticFields='true' or @Static='true']
       /VariableDeclarator/VariableDeclaratorId/@Image]]

Here's an example of code that would trigger this rule:

    private static Foo foo = null;

    //multiple simultaneous callers may see partially initialized objects
    public static Foo getFoo() {
        if (foo==null)
            foo = new Foo();
        return foo;
    }

This rule has the following properties:

Name | Default value | Description
--- | --- | ---
checkNonStaticMethods |  |
checkNonStaticFields |  |

### UncommentedEmptyMethod

Uncommented Empty Method finds instances where a method does not contain statements, but there is no comment. By explicitly commenting empty methods it is easier to distinguish between intentional (commented) and unintentional empty methods.

This rule is defined by the following XPath expression:

    //MethodDeclaration/Block[count(BlockStatement) = 0 and @containsComment = 'false']

Here's an example of code that would trigger this rule:

    public void doSomething() {
    }

### UncommentedEmptyConstructor

Uncommented Empty Constructor finds instances where a constructor does not contain statements, but there is no comment. By explicitly commenting empty constructors it is easier to distinguish between intentional (commented) and unintentional empty constructors.

This rule is defined by the following XPath expression:

    //ConstructorDeclaration[count(BlockStatement) = 0 and ($ignoreExplicitConstructorInvocation = 'true' or not(ExplicitConstructorInvocation)) and @containsComment = 'false']

Here's an example of code that would trigger this rule:

    public Foo() {
      super();
    }

This rule has the following properties:

Name | Default value | Description
--- | --- | ---
ignoreExplicitConstructorInvocation |  | Ignore explicit constructor invocation when deciding whether constructor is empty or not

### AvoidConstantsInterface

An interface should be used only to model a behaviour of a class: using an interface as a container of constants is a poor usage pattern.

This rule is defined by the following XPath expression:

        //ClassOrInterfaceDeclaration[@Interface="true"]
        [
         count(//MethodDeclaration)=0
         and
         count(//FieldDeclaration)&gt;0
        ]

Here's an example of code that would trigger this rule:

        public interface ConstantsInterface {
         public static final int CONSTANT1=0;
         public static final String CONSTANT2="1";
        }

### UnsynchronizedStaticDateFormatter

SimpleDateFormat is not synchronized. Sun recomends separate format instances for each thread. If multiple threads must access a static formatter, the formatter must be synchronized either on method or block level.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.design.UnsynchronizedStaticDateFormatter

Here's an example of code that would trigger this rule:

    public class Foo {
        private static final SimpleDateFormat sdf = new SimpleDateFormat();
        void bar() {
            sdf.format(); // bad
        }
        synchronized void foo() {
            sdf.format(); // good
        }
    }

### PreserveStackTrace

Throwing a new exception from a catch block without passing the original exception into the new Exception will cause the true stack trace to be lost, and can cause problems debugging problems

This rule is defined by the following Java class: net.sourceforge.pmd.rules.design.PreserveStackTrace

Here's an example of code that would trigger this rule:

    public class Foo {
        void good() {
            try{
                Integer.parseInt("a");
            } catch(Exception e){
                throw new Exception(e);
            }
        }
        void bad() {
            try{
                Integer.parseInt("a");
            } catch(Exception e){
                throw new Exception(e.getMessage());
            }
        }
    }