Get Warnings About Unused Method Arguments From Eclipse to Keep Code Clean
Join the DZone community and get the full member experience.
Join For Freeunused variables and methods should alway be unwelcome. removing them keeps the code cleaner and easier to read. now, by default eclipse warns you about unused private variables and methods, but it doesn’t warn you (by default) about unused method arguments.
but there is a compiler setting in eclipse that can warn you when you don’t use an argument in a method. you can even handle arguments on inherited methods, especially useful when using 3rd party libraries.
setup the compiler preferences
to get warnings of unused arguments:
- go to window > preferences > java > compiler > errors/warnings .
- open up the section unnecessary code .
- change the setting for parameter is never read from ignore to warning.
- (recommended, but optional) deselect ignore overriding and implementing methods . i recommend deselecting this option. you can leave it selected if you want to, but the feature loses a bit of its usefulness. i discuss this option a bit more in the next section.
here’s what the preference should look like:
and here’s an example of such a warning. in the example, the argument capacity isn’t used so is annotated as a warning.
what’s the easiest way to get rid of the warning? well, just remove the argument using the eclipse change method signature refactoring . this will remove the argument from the method declaration and any method callers in one go. however, if you can’t remove the argument from the method then read the next section.
on an old codebase you may get lots of warnings initially. i’d normally handle these on a class-by-class basis only for the classes i’m currently working on, unless the team has a specific cleanup project/task. just something to bear in mind, especially if you share workspace settings across the team via svn or similar.
what about method signatures that can’t be changed?
when you deselected ignore overriding and implementing methods, you told eclipse to warn you about unused arguments in implemented/overridden methods. it’s a good indicator that your superclass/interface method is passing in more than it needs to (ie. remove the argument from the method signature) or that you’re ignoring something important about the interface contract (ie. use it somewhere in your method).
but sometimes you can’t remove the argument because you don’t have control over inherited methods, especially if you implement/override a 3rd party library’s method or if the method is part of a bigger framework that’s difficult to change. other times you won’t have any need for the argument in very isolated cases. so you don’t want a warning to appear for these.
that’s why you can use the @suppresswarning compiler annotation to stop eclipse from reporting the warning. here’s an example:
public string process(@suppresswarnings("unused") int capacity, int max) {...}you can apply suppresswarning to an individual argument or the whole method. i’d recommend annotating only the argument that you want to ignore.
eclipse makes it easy to add the compiler annotation. just navigate to the warning , press ctrl+1 and choose add @suppresswarning ‘unused’ to ‘variable’ (quick fix does its job again).
from http://eclipseone.wordpress.com/2010/04/27/get-warnings-about-unused-method-arguments-from-eclipse/
Opinions expressed by DZone contributors are their own.
Comments