# Remove Unused Methods

# Description

This rule finds the method declarations that are never used and removes them. Provides options for removing methods that are only used in tests sources.

Save the API

Users are advised to be cautious when removing non-private methods from the source base. The public API defined in the project can be used by third parties that may not be present in the current workspace.

# Benefits

Some benefits of removing unused code, and in particular unused methods, are:

  • Reduces maintenance costs.
  • Reduces the build time.

# Configuration

This rule, together with Remove Unused Fields and Remove Unused Types, provides a dedicated configuration wizard that allows users to:

  • choose which methods to remove based on their access modifier. By default only the private modifier is selected.
  • choose the search scope for method references. It can either be set to Project or Workspace.
  • choose whether to remove methods that are only used in unit tests. In this case, the corresponding test cases are also removed. Users are advised to be cautions with this option as the test cases may cover more than the method being removed.

The following is a shot of the configuration wizard:

Remove unused code wizard

When clicking Finish, jSparrow will search for unused fields and methods (as described above), find and analyse their references, and compute the code changes.
The changes are shown in a Dif-View and users can choose to accept the computed changes for each unused method or field:

Remove unused code preview wizard

# Tags

# Code Changes

Pre

public class UnusedMethodsSample {

	private void unusedPrivateMethod() {}

	private void run() {
		System.out.println("Running...");
	}

	public static void main() {
		UnusedMethodsSample instance = new UnusedMethodsSample();
		instance.run();
	}

}

Post

public class UnusedMethodsSample {

	private void run() {
		System.out.println("Running...");
	}

	public static void main() {
		UnusedMethodsSample instance = new UnusedMethodsSample();
		instance.run();
	}

}

# Limitations

The method declarations that either override or are overridden by another method are not removed, even if they are never used.

🛠️ Auto-refactor Available

You can auto refactor this with jSparrow.
Drop this button to your Eclipse IDE workspace to install jSparrow for free:

Drag to your running Eclipse* workspace. *Requires Eclipse Marketplace Client

Need help? Check out our installation guide.

# Properties

Property Value
Rule ID RemoveUnusedMethods
First seen in jSparrow version 4.9.0
Minimum Java version 1.1
Remediation cost 2 min