Naming Conventions

Overview

Each of these naming modules validates identifiers for particular code elements. Valid identifiers for a naming module are specified by its format property. The value of format is a regular expression for valid identifiers. This is an example of a configuration of the MemberName module to ensure that member identifiers begin with 'm', followed by an upper case letter, and then letters and digits:

<module name="MemberName">
  <property name="format" value="^m[A-Z][a-zA-Z0-9]*$"/>
</module>

All naming modules belong to package com.puppycrawl.tools.checkstyle.checks.naming and are submodules of TreeWalker.

Modules

module validates identifiers for default value of format
AbstractClassName abstract classes ^Abstract.*$|^.*Factory$
ClassTypeParameterName class type parameters ^[A-Z]$
ConstantName constants (static, final fields) ^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$
LocalFinalVariableName local, final variables, including catch parameters ^[a-z][a-zA-Z0-9]*$
LocalVariableName local, non-final variables, including catch parameters ^[a-z][a-zA-Z0-9]*$
MemberName non-static fields ^[a-z][a-zA-Z0-9]*$
MethodName methods ^[a-z][a-zA-Z0-9]*$
MethodTypeParameterName method type parameters ^[A-Z]$
PackageName packages ^[a-z]+(\.[a-zA-Z_][a-zA-Z0-9_]*)*$
ParameterName parameters ^[a-z][a-zA-Z0-9]*$
StaticVariableName static, non-final fields ^[a-z][a-zA-Z0-9]*$
TypeName classes and interfaces ^[A-Z][a-zA-Z0-9]*$

Notes

The default value of format for module PackageName has been chosen to match the requirements in the Java Language specification and the Sun coding conventions. However both underscores and uppercase letters are rather uncommon, so most configurations should probably assign value ^[a-z]+(\.[a-z][a-z0-9]*)*$ to format for module PackageName, as in

<module name="PackageName">
    <property name="format"
              value="^[a-z]+(\.[a-z][a-z0-9]*)*$"/>
</module>

Module LocalVariableName also has property tokens which can be used to control whether the check applies to variable declarations or catch clause parameters through tokens VARIABLE_DEF and PARAMETER_DEF. For example, the following configuration element ensures that catch clause parameters begin with "e", followed by letters and digits:

<module name="LocalVariableName">
    <property name="format" value="^e[a-zA-Z0-9]*$"/>
    <property name="tokens" value="PARAMETER_DEF"/>
</module>

Module TypeName also has property tokens which can be used to control whether the check applies to classes or interfaces through tokens CLASS_DEF and INTERFACE_DEF. For example, the following configuration element ensures that interface names begin with "I_", followed by letters and digits:

<module name="TypeName">
    <property name="format"
              value="^I_[a-zA-Z0-9]*$"/>
    <property name="tokens"
              value="INTERFACE_DEF"/>
</module>

The modules ConstantName, MemberName, StaticVariableName and TypeName also have the following properties:

name description type default value
applyToPublic Controls whether to apply the check to public member. Boolean true
applyToProtected Controls whether to apply the check to protected member. Boolean true
applyToPackage Controls whether to apply the check to package-private member. Boolean true
applyToPrivate Controls whether to apply the check to private member. Boolean true

Module MethodName also has the following properties:

name description type default value
allowClassName Controls whether to allow a method name to have the same name as the residing class name. This is not to be confused with a constructor. An easy mistake is to place a return type on a constructor declaration which turns it into a method. For example:
class MyClass {
    public void MyClass() {} //this is a method
    public MyClass() {} //this is a constructor
}
            
Boolean false
Copyright © 2001-2010, Oliver Burn