3

An operation in an interface is obsolete so I decided to delete it. It seems that there is no automatic support for such a "refactoring" in Eclipse.

For me is a refactoring operation since the behavior of the code will be preserved since nobody (tests, client apis) will notice that the operation was removed.

In Eclipse, in Java code, on an method in an interface I have the following options: rename, move, change method signature, inline, extract interface, extract superclass, use supertype when possible, pull up, push down, introduce parameter objet, introduce indirection, generate declared type.

Is there any reason for which a delete method/field/function refactoring doesn't exist?

  • 4
    IntelliJ IDEA has this refactoring as "Safe delete". – msell Oct 26 '13 at 14:35
  • 1
    I could only possibly see this working if the method returns void, and that's normally a fairly small subset of methods in OOP. What would you want the IDE to do if something was depending on the return value? – Aaronaught Oct 26 '13 at 15:21
  • @Aaronaught Can you explain that? If no one uses a method who cares what it returns – jozefg Oct 26 '13 at 17:58
  • 1
    @jozefg: The OP said that "nobody will notice that the operation was removed", which is not the same as saying that it's not used anywhere - presumably the assumption is that the operation could be safely turned into a no-op. If it's truly not referenced anywhere else, then a special-purpose "refactoring" tool would be pointless because it would just remove one line of code. It's faster to just, you know, remove it. – Aaronaught Oct 26 '13 at 21:45
  • 1
    There are no calls but this method is in an interface that is "implemented" by several other classes. So when I decide to delete a method I must delete it from interface and go in all these implementations to manually delete it. – raisercostin Nov 1 '13 at 19:00
7

The reason is, this feature is not yet implemented - see Bug 39715 Add Refactor/Remove interface method and all impls. [refactoring] in Eclipse issue tracker:

Here is something I am running into now. I am cleaning up a bunch of code that removes no longer needed methods from interfaces and all implementing classes. Right now, this is quite a tedious processes. It would be helpful to have a refactoring such that I could select an interface method and delete it along with all impl. methods in all classes...

Above request has been in turn marked a duplicate of Bug 24379 [refactoring] Change interface signature:

When adding or removing methods in interfaces it would nice to have the possibility to add or remove the method automatically in implementing classes. When adding methods it should also add a task with the note implement this method...

Bug 24379 seems to be laying dormant for more than 10 years now, "Reported: 2002-10-04" and "Target Milestone" field is not yet set.

4

There are only two situations when removing a method from an interface: either the method is being used somewhere, or it's not. If the method is never used, then deleting it is trivial even without a refactoring tool.

If the method is used, then there would almost never be a way to automatically remove it in every place that it is used. Simply deleting it everywhere would surely break things. The best that an IDE could do would be to give you a list of everywhere the method is used and let you decide how to safely remove the method in each case. IDEs like Eclipse will do that for you.

In short, a refactoring tool for deleting methods would either be useless because it is trivial, or useless because it breaks things, depending on which method you use it on.

  • 2
    As an IntelliJ IDEA user I have to disagree with this answer. This refactoring tool exists and I use it somewhat often. When there are no usages of a method, it can still be tedious to delete it manually due to inheritance or interfaces. If there are usages, the tool also helps on resolving these cases. And it removes the extra step for finding the usages first. – msell Nov 27 '13 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.