Sundarrajk's Weblog

PMD – Unused Code Checks

Posted on: September 28, 2011


This is the twentieth installment of explanation of PMD rules covering Unused Code Checks.

Unused Code Rules

The Unused Code Ruleset contains a collection of rules that find unused code.

UnusedPrivateField

Detects when a private field is declared and/or assigned a value, but not used.

Example

public class Something {
  private static int FOO = 2; // Unused
  private int i = 5; // Unused
  private int j = 6;
  public int addOne() {
    return j++;
  }
}

UnusedLocalVariable

Detects when a local variable is declared and/or assigned, but not used.

Example

public class Foo {
public void doSomething() {
  int i = 5; // Unused
}
}

UnusedPrivateMethod

Unused Private Method detects when a private method is declared but is unused.

Example

public class Something {
private void foo() {} // unused
}

UnusedFormalParameter

Avoid passing parameters to methods or constructors and then not using those parameters.

Example

public class Foo {
private void bar(String howdy) {
  // howdy is not used
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Categories

Advertisements
%d bloggers like this: