Naming Rules

The Naming Ruleset contains a collection of rules about names - too long, too short, and so forth.

ShortVariable

Since: PMD0.3

Detects when a field, local, or parameter has a very short name.

                  
//VariableDeclaratorId[string-length(@Image) < 3]
 [not(ancestor::ForInit)]
 [not((ancestor::FormalParameter) and (ancestor::TryStatement))]
                  
              
Example(s):

public class Something {
  private int q = 15; // VIOLATION - Field
  public static void main( String as[] ) {  // VIOLATION - Formal
    int r = 20 + q; // VIOLATION - Local
    for (int i = 0; i < 10; i++) { // Not a Violation (inside FOR)
      r += q;
    }
  }
}

    

LongVariable

Since: PMD0.3

Detects when a field, formal or local variable is declared with a long name.

                  
//VariableDeclaratorId[string-length(@Image) > $minimum]
                  
              
Example(s):

public class Something {
  int reallyLongIntName = -3;  // VIOLATION - Field
  public static void main( String argumentsList[] ) { // VIOLATION - Formal
    int otherReallyLongName = -5; // VIOLATION - Local
    for (int interestingIntIndex = 0;  // VIOLATION - For
             interestingIntIndex < 10;
             interestingIntIndex ++ ) {
    }
}

    

This rule has the following properties:

Name Default Value Description
minimum 17 The variable length reporting threshold

ShortMethodName

Since: PMD0.3

Detects when very short method names are used.

                  
//MethodDeclarator[string-length(@Image) < 3]
                  
              
Example(s):

public class ShortMethod {
  public void a( int i ) { // Violation
  }
}

     

VariableNamingConventions

Since: PMD1.2

A variable naming conventions rule - customize this to your liking. Currently, it checks for final variables that should be fully capitalized and non-final variables that should not include underscores.

This rule is defined by the following Java class:net.sourceforge.pmd.lang.java.rule.naming.VariableNamingConventionsRule

Example(s):

public class Foo {
 public static final int MY_NUM = 0;
 public String myTest = "";
 DataModule dmTest = new DataModule();
}

        

MethodNamingConventions

Since: PMD1.2

Method names should always begin with a lower case character, and should not contain underscores.

This rule is defined by the following Java class:net.sourceforge.pmd.lang.java.rule.naming.MethodNamingConventionsRule

Example(s):

public class Foo {
 public void fooStuff() {
 }
}

          

ClassNamingConventions

Since: PMD1.2

Class names should always begin with an upper case character.

This rule is defined by the following Java class:net.sourceforge.pmd.lang.java.rule.naming.ClassNamingConventionsRule

Example(s):

public class Foo {}

      

AbstractNaming

Since: PMD1.4

Abstract classes should be named 'AbstractXXX'.

                    
//ClassOrInterfaceDeclaration
 [@Abstract='true' and @Interface='false']
 [not (starts-with(@Image,'Abstract'))]
                    
                
Example(s):

public abstract class Foo { // should be AbstractFoo
}

       

AvoidDollarSigns

Since: PMD1.5

Avoid using dollar signs in variable/method/class/interface names.

This rule is defined by the following Java class:net.sourceforge.pmd.lang.java.rule.naming.AvoidDollarSignsRule

Example(s):
   
 public class Fo$o {  // yikes!
 }
   
       

MethodWithSameNameAsEnclosingClass

Since: PMD1.5

Non-constructor methods should not have the same name as the enclosing class.

This rule is defined by the following Java class:net.sourceforge.pmd.lang.java.rule.naming.MethodWithSameNameAsEnclosingClassRule

Example(s):
    
public class MyClass {
 // this is bad because it is a method
 public void MyClass() {}
 // this is OK because it is a constructor
 public MyClass() {}
}
    
       

SuspiciousHashcodeMethodName

Since: PMD1.5

The method name and return type are suspiciously close to hashCode(), which may mean you are intending to override the hashCode() method.

This rule is defined by the following Java class:net.sourceforge.pmd.lang.java.rule.naming.SuspiciousHashcodeMethodNameRule

Example(s):
    
public class Foo {
 public int hashcode() {
 // oops, this probably was supposed to be hashCode
 }
}
    
       

SuspiciousConstantFieldName

Since: PMD2.0

A field name is all in uppercase characters, which in Sun's Java naming conventions indicate a constant. However, the field is not final.


//ClassOrInterfaceDeclaration[@Interface='false']
 /ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/FieldDeclaration
  [@Final='false']
  [VariableDeclarator/VariableDeclaratorId[upper-case(@Image)=@Image]]
 
                
Example(s):
    
public class Foo {
 // this is bad, since someone could accidentally
 // do PI = 2.71828; which is actualy e
 // final double PI = 3.16; is ok
 double PI = 3.16;
}
    
       

SuspiciousEqualsMethodName

Since: PMD2.0

The method name and parameter number are suspiciously close to equals(Object), which may mean you are intending to override the equals(Object) method.

        
//MethodDeclarator[
(
@Image = 'equals'
  and count(FormalParameters/*) = 1
  and not (FormalParameters/FormalParameter/Type/ReferenceType/ClassOrInterfaceType
   [@Image = 'Object' or @Image = 'java.lang.Object'])
)
or
@Image='equal'
 and count(FormalParameters/*) = 1
 and (FormalParameters/FormalParameter/Type/ReferenceType/ClassOrInterfaceType
  [@Image = 'Object' or @Image = 'java.lang.Object'])

]
        
                    
Example(s):
        
public class Foo {
 public int equals(Object o) {
 // oops, this probably was supposed to be boolean equals
 }
 public boolean equals(String s) {
 // oops, this probably was supposed to be equals(Object)
 }
}
        
        

AvoidFieldNameMatchingTypeName

Since: PMD3.0

It is somewhat confusing to have a field name matching the declaring class name. This probably means that type and or field names could be more precise.

This rule is defined by the following Java class:net.sourceforge.pmd.lang.java.rule.naming.AvoidFieldNameMatchingTypeNameRule

Example(s):

public class Foo extends Bar {
 // There's probably a better name for foo
 int foo;
}

      

AvoidFieldNameMatchingMethodName

Since: PMD3.0

It is somewhat confusing to have a field name with the same name as a method. While this is totally legal, having information (field) and actions (method) is not clear naming.

This rule is defined by the following Java class:net.sourceforge.pmd.lang.java.rule.naming.AvoidFieldNameMatchingMethodNameRule

Example(s):

public class Foo {
	Object bar;
	// bar is data or an action or both?
	void bar() {
	}
}

      

NoPackage

Since: PMD3.3

Detects when a class or interface does not have a package definition.

                  
//ClassOrInterfaceDeclaration[count(preceding::PackageDeclaration) = 0]
                  
              
Example(s):

// no package declaration
public class ClassInDefaultPackage {
}

    

PackageCase

Since: PMD3.3

Detects when a package definition contains upper case characters.

                      
//PackageDeclaration/Name[lower-case(@Image)!=@Image]
                      
                  
Example(s):
    
package com.MyCompany;  // <- should be lower case name
public class SomeClass {
}
    
        

MisleadingVariableName

Since: PMD3.4

Detects when a non-field has a name starting with 'm_'. This usually indicates a field and thus is confusing.

                    
//VariableDeclaratorId
[starts-with(@Image, 'm_')]
[not (../../../FieldDeclaration)]
                    
                
Example(s):
  
  public class Foo {
    private int m_foo; // OK
    public void bar(String m_baz) {  // Bad
      int m_boz = 42; // Bad
    }
  }
  
      

BooleanGetMethodName

Since: PMD4.0

Looks for methods named 'getX()' with 'boolean' as the return type. The convention is to name these methods 'isX()'.

                    
//MethodDeclaration[
MethodDeclarator[count(FormalParameters/FormalParameter) = 0 or $checkParameterizedMethods = 'true']
                [starts-with(@Image, 'get')]
and
ResultType/Type/PrimitiveType[@Image = 'boolean']
]

                
Example(s):
            
public boolean getFoo(); // bad
public boolean isFoo(); // ok
public boolean getFoo(boolean bar); // ok, unless checkParameterizedMethods=true
     

This rule has the following properties:

Name Default Value Description
checkParameterizedMethods Check parameterized methods

ShortClassName

Since: PMD5.0

Detects if a classname if lower than 5 characters.

                      
//ClassOrInterfaceDeclaration[string-length(@Image) < 5]
                      
                  
Example(s):
    
public class Foo {
}