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]$
InterfaceTypeParameterName interface 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, interfaces, enums, and annotations ^[A-Z][a-zA-Z0-9]*$

Enhancements

LocalVariableName

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>
        

The check provides the following properties:

name description type default value
allowOneCharVarInForLoop Allow one character variable name in initialization expressions in FOR loop. For example:
for (int i = 1; i < 10; i++) {}
              
Boolean false

An example of how to configure the check to allow one character variable name in initialization expressions in FOR loop:

<module name="LocalVariableName">
    <property name="allowOneCharVarInForLoop" value="true"/>
</module>
        

TypeName

Module TypeName also has property tokens, which can be used to control the kind of type that the check applies to. The value of the tokens property is a comma-separated list of one or more of the following tokens: CLASS_DEF, INTERFACE_DEF, ENUM_DEF, ANNOTATION_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>
        

MethodName

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

AbstractClassName

Module AbstractClassName also has the following properties:

name description type default value
ignoreModifier Controls whether to ignore checking for the abstract modifier on classes that match the name. Boolean false
ignoreName Controls whether to ignore checking the name. Realistically only useful if using the check to identify that match name and do not have the abstract modifier. name. Boolean false

The following example shows how to configure the AbstractClassName to checks names, but ignore missing abstract modifiers:

<module name="AbstractClassName">
  <property name="ignoreModifier" value="true"/>
</module>
        

ConstantName

Property format in module ConstantName is used to specify names to be allowed. The following configuration apart from names allowed by default allows log or logger:

<module name="ConstantName">
    <property name="format"
              value="^log(ger)?|[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"/>
</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

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>
      

AbbreviationAsWordInName

Description

The Check validate abbreviations(consecutive capital letters) length in identifier name, it also allows to enforce camel case naming. Please read more at Google Style Guide to get to know how to avoid long abbreviations in names.

Properties

name description type default value
allowedAbbreviationLength indicates on the allowed amount of capital letters in targeted identifiers (abbreviations in the classes, interfaces, variables and methods names, ... ). 3 true
allowedAbbreviations list of abbreviations that must be skipped for checking. Abbreviations should be separated by comma, no spaces are allowed. stringSet null
ignoreFinal allow to skip variables with final modifier. Boolean true
ignoreStatic allow to skip variables with static modifier. Boolean true
ignoreOverriddenMethod Allows to ignore methods tagged with @Override annotation (that usually mean inherited name). Boolean true

Examples

Default configuration

<module name="AbbreviationAsWordInName"/>
         

To configure to check variables and classes identifiers, do not ignore variables with static modifier and allow no abbreviations (enforce camel case phrase) and allow no abbreviations to use (camel case phrase) and allow XML and URL abbreviations.

<module name="AbbreviationAsWordInName">
    <property name="tokens" value="VARIABLE_DEF,CLASS_DEF"/>
    <property name="ignoreStatic" value="false"/>
    <property name="allowedAbbreviationLength" value="1"/>
    <property name="allowedAbbreviation" value="XML,URL"/>
</module>
        

Package

com.puppycrawl.tools.checkstyle.checks.naming

Parent Module

TreeWalker