ParameterName

Since Checkstyle 3.0

Description

Checks that method parameter names conform to a specified pattern. By using accessModifiers property it is possible to specify different formats for methods at different visibility levels.

To validate catch parameters please use CatchParameterName .

To validate lambda parameters please use LambdaParameterName .

Properties

name description type default value since
accessModifiers Access modifiers of methods where parameters are checked. AccessModifierOption[] public, protected, package, private 7.5
format Sets the pattern to match valid identifiers. Pattern "^[a-z][a-zA-Z0-9]*$" 3.0
ignoreOverridden Allows to skip methods with Override annotation from validation. boolean false 6.12.1

Examples

To configure the check:

<module name="Checker">
  <module name="TreeWalker">
    <module name="ParameterName"/>
  </module>
</module>
        

Code Example:

class Example1 {
  void method1(int v1) {}
  void method2(int V2) {} // violation
}
        

An example of how to configure the check for names that begin with a lower case letter, followed by letters, digits, and underscores:

<module name="Checker">
  <module name="TreeWalker">
    <module name="ParameterName">
      <property name="format" value="^[a-z][_a-zA-Z0-9]+$"/>
    </module>
  </module>
</module>
        

Code Example:

class Example2 {
  void method1(int v1) {}
  void method2(int v_2) {}
  void method3(int V3) {} // violation
}
        

An example of how to configure the check to skip methods with Override annotation from validation:

<module name="Checker">
  <module name="TreeWalker">
    <module name="ParameterName">
      <property name="ignoreOverridden" value="true"/>
    </module>
  </module>
</module>
        

Code Example:

class Example3 {
  void method1(int v1) {}
  void method2(int V2) {} // violation
  @Override
  public boolean equals(Object V3) { // OK
    return true;
  }
}
        

An example of how to configure the check for names that begin with a lower case letter, followed by letters and digits is:

<module name="Checker">
  <module name="TreeWalker">
    <module name="ParameterName">
      <property name="format" value="^[a-z][a-zA-Z0-9]+$"/>
    </module>
  </module>
</module>
        

Code Example:

class Example4 {
  void method1(int v1) {}
  void method2(int v_2) {} // violation
  void method3(int V3) {} // violation
}
        

The following configuration checks that the parameters always start with two lowercase characters and, in addition, that public method parameters cannot be one character long:

<module name="Checker">
  <module name="TreeWalker">
    <module name="ParameterName">
      <property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
      <property name="accessModifiers" value="protected, package, private"/>
      <message key="name.invalidPattern"
        value="Parameter name ''{0}'' must match pattern ''{1}''"/>
    </module>
    <module name="ParameterName">
      <property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9]*$"/>
      <property name="accessModifiers" value="public"/>
      <message key="name.invalidPattern"
        value="Parameter name ''{0}'' must match pattern ''{1}''"/>
    </module>
  </module>
</module>
        

Code Example:

class Example5 {
  void fn1(int v1) {}
  protected void fn2(int V2) {} // violation "Parameter name 'V2' must match pattern"
  private void fn3(int a) {}
  public void fn4(int b) {} // violation "Parameter name 'b' must match pattern"
}
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.naming

Parent Module

TreeWalker