Naming Rules

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

ShortVariable

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

This rule is defined by the following XPath expression:

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

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

			

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

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

This rule is defined by the following XPath expression:

                  
    //VariableDeclaratorId[string-length(@Image) > 17]
                  
              

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

			

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 ++ ) {

    }
}


    
		

ShortMethodName

Detects when very short method names are used.

This rule is defined by the following XPath expression:

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

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

			

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

     
		

VariableNamingConventions

A variable naming conventions rule - customize this to your liking Final variables should be all caps Non-final variables should not include underscores

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

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

			

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

        
		

MethodNamingConventions

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.rules.MethodNamingConventions

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

			

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

          
		

ClassNamingConventions

Class names should always begin with an upper case character.

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

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

			

public class Foo {}

      
		

AbstractNaming

Abstract classes should be named 'AbstractXXX'.

This rule is defined by the following XPath expression:

                    
//ClassOrInterfaceDeclaration
 [@Abstract='true' and @Interface='false']
 [starts-with(@Image,'Abstract') = 0]
                    
                

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

			

public abstract class Foo { // should be AbstractFoo
}

       
		

AvoidDollarSigns

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

This rule is defined by the following XPath expression:


//ClassOrInterfaceDeclaration[contains(@Image, '$')]
|
//VariableDeclaratorId[contains(@Image, '$')]
|
//MethodDeclarator[contains(@Image, '$')]
 
                 

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

			
   
 public class Fo$o {  // yikes!
 }
   
       
		

MethodWithSameNameAsEnclosingClass

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.rules.MethodWithSameNameAsEnclosingClass

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

			
    
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

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

This rule is defined by the following XPath expression:


//MethodDeclaration
 [ResultType
  //PrimitiveType
   [@Image='int']
   [//MethodDeclarator
    [@Image='hashcode' or @Image='HashCode' or @Image='Hashcode']
    [not(FormalParameters/*)]

                

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

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

SuspiciousConstantFieldName

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

This rule is defined by the following XPath expression:


//ClassOrInterfaceDeclaration[@Interface='false']
 /ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/FieldDeclaration
  [@Final='false']
  [VariableDeclarator/VariableDeclaratorId[upper-case(@Image)=@Image]]
 
                

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

			
    
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

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

This rule is defined by the following XPath expression:

        
//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'])

]
        
                    

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

			
        
    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

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

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

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

			

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

      
		

AvoidFieldNameMatchingMethodName

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.rules.AvoidFieldNameMatchingMethodName

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

			

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

      
		

AvoidNonConstructorMethodsWithClassName

It is very easy to confuse methods with classname with constructors. It is preferrable to name these non-constructor methods in a different way.

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

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

			

public class Foo {
	public void Foo() {
		// not really a constructor...
	}
}