Local variables and method parameters should be named consistently to communicate intent and improve maintainability. Rename your local variable or method parameter to follow your project’s naming convention to address this issue.

Why is this an issue?

A naming convention in software development is a set of guidelines for naming code elements like variables, functions, and classes.
Local variables and method parameters hold the meaning of the written code. Their names should be meaningful and follow a consistent and easily recognizable pattern.
Adhering to a consistent naming convention helps to make the code more readable and understandable, which makes it easier to maintain and debug. It also ensures consistency in the code, especially when multiple developers are working on the same project.

This rule checks that local variable and method parameter names match a provided regular expression.

What is the potential impact?

Inconsistent naming of local variables and method parameters can lead to several issues in your code:

In summary, not adhering to a naming convention for local variables and method parameters can lead to confusion, errors, and inefficiencies, making the code harder to read, understand, and maintain.

Exceptions

Loop counters are ignored by this rule.

for (int i_1 = 0; i_1 < limit; i_1++) {  // Compliant
  // ...
}

as well as one-character catch variables:

try {
//...
} catch (Exception e) { // Compliant
}

Local variables inside a method body and named lambda parameters may have one or more leading underscores, trailing underscores, or both. This convention signals that the variable is temporary, unimportant, or intentionally unnamed. The name between the underscores must still match the configured format.

public class MyClass {
    public void doSomething() {
      int _result = compute();                    // Compliant
      int value__ = 0;                            // Compliant
      int __tmp__ = swap();                       // Compliant
      IntUnaryOperator f = _value -> _value + 1; // Compliant - lambda parameter
    }
}

Note that this exception applies only to local variables declared inside a method body and to named lambda parameters. Method parameters and constructor parameters must match the configured format without leading or trailing underscores.

How to fix it

First, familiarize yourself with the particular naming convention of the project in question. Then, update the name to match the convention, as well as all usages of the name. For many IDEs, you can use built-in renaming and refactoring features to update all usages at once.

Code examples

Noncompliant code example

With the default regular expression ^[a-z][a-zA-Z0-9]*$:

public class MyClass {
    public void doSomething(int _myParam) {  // Noncompliant - method parameters cannot have leading or trailing underscores
      int LOCAL;    // Noncompliant - local variable does not match the naming convention
      int __BAD__;  // Noncompliant - inner part "BAD" does not match the naming convention
      // ...
    }
}

Compliant solution

public class MyClass {
    public void doSomething(int myParam) {  // Compliant
      int local;      // Compliant
      int _local;     // Compliant - leading underscore allowed for local variables
      int result__;   // Compliant - trailing underscores allowed for local variables
      int __tmp__;    // Compliant - leading and trailing underscores allowed for local variables
      // ...
    }
}

Resources

Documentation

Related rules