0

I need to fix a bug in my project, but it turns out the root cause is an effect of many workarounds spread for all implementations of an interface due to a design problem. I want to refactor that interface, but I can't do it now, cause I don't have time to change all implementations. So my ideia is to add a default method in this interface and implement it in only one implementation (partially fixing the design problem) and then making the big refactoring in next sprint. This refactoring is about replacing all methods in this interface for simpler and more meaningful ones. One of those new methods is the default method which the question is about.

But actually there is no implementation needed for this method, its just a temporary solution which goes to the right direction.

Does it make sense to implementing that default method (in the interface, of course) throwing an UnsupportedOperationException?

Diego Marin Santos
  • 1,923
  • 2
  • 15
  • 29
  • 1
    I don't see how adding a default method could help with a situation such as you describe. If you add a new method with a default implementation, then none of your existing code invokes it, because it's a new method. If you add a default implementation for an existing method, then none of your other existing code invokes it, because the implementations in use now necessarily include their own implementation of the method in question. – John Bollinger Feb 05 '18 at 17:11
  • I'm gonna override it in the implementation class that I need to fix the bug and then call this new method instead of the previous one which doesn't make sense. Sorry, I think the question wasn't clear enough. I updated it. – Diego Marin Santos Feb 05 '18 at 17:17
  • If the client code works with interface only then how you are going to make sure it wont call the new method on implementation that throws exception? If its working against a concrete implementation then why add the method on interface if you can add it to the implementation class? – tsolakp Feb 05 '18 at 17:22
  • Client code is calling the interface and the bug is in one of the implementations. But the method itself doesn't make sense, so if I fix the implementation code the method name and its implementation will look like nonsense. – Diego Marin Santos Feb 05 '18 at 17:27

1 Answers1

1

The whole idea behind default methods is to provide for adding methods to an interface without automatically breaking existing implementations. It seems like that is what you propose to do, so that makes sense as far as it goes.

But in comments you remarked:

I'm gonna override it in the implementation class that I need to fix the bug and then call this new method instead of the previous one which doesn't make sense.

If you are going to be invoking the new method on any instance of your interface, then you need to be confident that it will do something appropriate. If there's any chance that such an invocation is served by your proposed default implementation that always throws UnsupportedOperationException (and supposing that would be undesirable), then that probably does not justify such confidence.

If you somehow do have justified confidence that the new method will be invoked only on instances of one specific implementation class then either there is something very strange about the way you are using interfaces, or you don't actually need to change the interface at all. That is, if you know what implementation you are working with, you can add the new method to that class alone, without changing the interface.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Thanks for your answer. Yes, I'm confident it will be called only on one specific implementation class cause I have control over the injection of that dependency in the client. In addition, it's only a temporary solution which is trying to be closer to the right design. I agree that the refactoring which will replace all methods should be done asap, so I will remove the default modifier from that method. – Diego Marin Santos Feb 05 '18 at 17:50