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

Use a Java Refactoring Tool

No license required

You can review this refactoring on your code without a license by installing jSparrow to your Eclipse IDE. Install the plug-in from Eclipse IDE: Eclipse Marketplace.

System-wide Refactoring

Do you want to automate this refactoring (and many more) to your system-wide code? The automatic application of this system-wide refactoring can be unlocked by acquiring your jSparrow license.

a drawn cute bird pointing at a graph that shows positive results

# Properties

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