VariableDeclarationUsageDistance

Since Checkstyle 5.8

Description

Checks the distance between declaration of variable and its first usage. Note : Variable declaration/initialization statements are not counted while calculating length.

Properties

name description type default value since
allowedDistance Specify distance between declaration of variable and its first usage. Values should be greater than 0. int 3 5.8
ignoreFinal Allow to ignore variables with a 'final' modifier. boolean true 5.8
ignoreVariablePattern Define RegExp to ignore distance calculation for variables listed in this pattern. Pattern "" 5.8
validateBetweenScopes Allow to calculate the distance between declaration of variable and its first usage in the different scopes. boolean false 5.8

Examples

To configure the check with default config:

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

Example:

public class Example1 {

  public void foo1() {
    int num;        // violation, distance = 4
    final double PI;   // OK, final variables not checked
    System.out.println("Statement 1");
    System.out.println("Statement 2");
    System.out.println("Statement 3");
    num = 1;
    PI = 3.14;
  }

  public void foo2() {
    int a;          // OK, used in different scope
    int b;          // OK, used in different scope
    int count = 0;  // OK, used in different scope

    {
      System.out.println("Inside inner scope");
      a = 1;
      b = 2;
      count++;
    }
  }
}
        

Check can detect a block of initialization methods. If a variable is used in such a block and there are no other statements after variable declaration, then distance = 1.

Case #1:

int minutes = 5;
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timeNow);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.HOUR_OF_DAY, hh);
cal.set(Calendar.MINUTE, minutes);
        

The distance for the variable "minutes" is 1 even though this variable is used in the fifth method's call.

Case #2:

int minutes = 5;
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timeNow);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
System.out.println(cal);
cal.set(Calendar.HOUR_OF_DAY, hh);
cal.set(Calendar.MINUTE, minutes);
        

The distance for the variable "minutes" is 6 because there is one more expression (except the initialization block) between the declaration of this variable and its usage.

To configure the check to set allowed distance:

<module name="Checker">
  <module name="TreeWalker">
    <module name="VariableDeclarationUsageDistance">
      <property name="allowedDistance" value="4"/>
    </module>
  </module>
</module>
        

Example:

public class Example2 {

  public void foo1() {
    int num;        // OK, distance = 4
    final double PI;   // OK, final variables not checked
    System.out.println("Statement 1");
    System.out.println("Statement 2");
    System.out.println("Statement 3");
    num = 1;
    PI = 3.14;
  }

  public void foo2() {
    int a;          // OK, used in different scope
    int b;          // OK, used in different scope
    int count = 0;  // OK, used in different scope

    {
      System.out.println("Inside inner scope");
      a = 1;
      b = 2;
      count++;
    }
  }
}
        

To configure the check to ignore certain variables:

<module name="Checker">
  <module name="TreeWalker">
    <module name="VariableDeclarationUsageDistance">
      <property name="ignoreVariablePattern" value="^num$"/>
    </module>
  </module>
</module>
        

This configuration ignores variables named "num".

Example:

public class Example3 {

  public void foo1() {
    int num;        // OK, variable ignored
    final double PI;   // OK, final variables not checked
    System.out.println("Statement 1");
    System.out.println("Statement 2");
    System.out.println("Statement 3");
    num = 1;
    PI = 3.14;
  }

  public void foo2() {
    int a;          // OK, used in different scope
    int b;          // OK, used in different scope
    int count = 0;  // OK, used in different scope

    {
      System.out.println("Inside inner scope");
      a = 1;
      b = 2;
      count++;
    }
  }
}
        

To configure the check to force validation between scopes:

<module name="Checker">
  <module name="TreeWalker">
    <module name="VariableDeclarationUsageDistance">
      <property name="validateBetweenScopes" value="true"/>
    </module>
  </module>
</module>
        

Example:

public class Example4 {

  public void foo1() {
    int num;        // violation, distance = 4
    final double PI;   // OK, final variables not checked
    System.out.println("Statement 1");
    System.out.println("Statement 2");
    System.out.println("Statement 3");
    num = 1;
    PI = 3.14;
  }

  public void foo2() {
    int a;          // OK, distance = 2
    int b;          // OK, distance = 3
    int count = 0;  // violation, distance = 4

    {
      System.out.println("Inside inner scope");
      a = 1;
      b = 2;
      count++;
    }
  }
}
        

To configure the check to check final variables:

<module name="Checker">
  <module name="TreeWalker">
    <module name="VariableDeclarationUsageDistance">
      <property name="ignoreFinal" value="false"/>
    </module>
  </module>
</module>
        

Example:

public class Example5 {

  public void foo1() {
    int num;        // violation, distance = 4
    final double PI;   // violation, distance = 5
    System.out.println("Statement 1");
    System.out.println("Statement 2");
    System.out.println("Statement 3");
    num = 1;
    PI = 3.14;
  }

  public void foo2() {
    int a;          // OK, used in different scope
    int b;          // OK, used in different scope
    int count = 0;  // OK, used in different scope

    {
      System.out.println("Inside inner scope");
      a = 1;
      b = 2;
      count++;
    }
  }
}
        

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.coding

Parent Module

TreeWalker