VariableDeclarationUsageDistance

Since Checkstyle 5.8

Description

Checks the distance between declaration of variable and its first usage. Note: Any additional variables declared or initialized between the declaration and the first usage of the said variable are not counted when calculating the distance.

Properties

name description type default value since
allowedDistance Specify the maximum distance between a variable's declaration and its first usage. Value 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 a variable's declaration and its first usage across 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() {
    // violation below, 'variable 'num' declaration and its first usage is 4.'
    int num;
    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 is able to detect a block of initialization methods, as a single point for distance. If a variable is used in such a block and there are no other statements after variable declaration, then distance = 1.

Configure the check to default:


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

Example:


public class Example2 {

  public void case1(long timeNow, int hh, int min) {
    int minutes = min + 5; // ok, No violation reported
    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);
  }

  public void case2(long timeNow, int hh, int min){
    // violation below, 'variable 'minutes' declaration and its first usage is 6.'
    int minutes = min + 5000;
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(timeNow);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    cal.set(Calendar.HOUR_OF_DAY, hh);
    System.out.println("Hello World");
    cal.set(Calendar.MINUTE, minutes);
  }
}

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

The distance for the variable "minutes" in the method case2 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 Example3 {

  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 Example4 {

  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 Example5 {

  public void foo1() {
    // violation below, 'variable 'num' declaration and its first usage is 4.'
    int num;
    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
    // violation below, 'variable 'count' declaration and its first usage is 4.'
    int count = 0;

    {
      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 Example6 {

  public void foo1() {
    // violation below, 'variable 'num' declaration and its first usage is 4.'
    int num;
    // violation below, 'variable 'PI' declaration and its first usage is 5.'
    final double PI;
    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