1

I'm working with a Java project that has several interfaces, many of which have only one implementation. (See related question)

For a given revision of the software, one could think this acceptable because the interfaces were some plan for future extensions. However, when I look at the 200+ revisions over several years, those interfaces never had more than one implementation, (nor were they used in unit-testing).

It seems that in these cases of probable bloat (YAGNI) owing to the Interface Anti-pattern, it would be useful to apply the inverse of Extract Interface. However, Inline Class (the supposed inverse according to refactoring.com) is not what I'm referring to.

What's the name of the refactoring that removes an unused interface, substituting the sole class that implements it?

3
  • Merge interface (on the expectation that the class has at least some other members)?
    – david.pfx
    Mar 22, 2014 at 14:09
  • I found this blog that speaks of Merge Interface and Implementation (and it's spot-on to the gist of my question). It also states that no popular IDEs offer this refactoring, which also kind of answers my question at the same time. @david.pfx I recommend you submit your remark as an answer. Mar 22, 2014 at 17:22
  • Frame challenge: Just because I didn't have a car crash doesn't mean I wasted effort on wearing a seatbelt. Even if I haven't ever crashed a car in years, that still doesn't mean that wearing a seatbelt is unnecessarily wasted effort. I'd rather write 10 interfaces that never end up diverging in the end than rewiring 1 concrete class' references across the entire codebase to now refer an interface because the codebase expanded. Doubly so if I once had the interface but then actively threw it away (which now ends up costing triple the effort: create-destroy-create).
    – Flater
    Nov 23, 2021 at 16:51

2 Answers 2

1

Merge Interface (on the expectation that the class has at least some other members)?

As suggested, Merge Interface and Implementation would be even more descriptive. I don't recall ever seeing it in an IDE.

1
  • 1
    Resharper (and probably all JetBrains IDEs) have 'Safe Delete' which lets you replace usages with an implementation. Jan 16, 2019 at 15:36
1

Going by the naming conventions I've seen (especially on refactoring.guru), I'd recommend the simple description of Replace interface with class. As you describe it, it's functionally equivalent to inlining, but I agree that it doesn't quite make sense to be called inlining (for both Inline method and Inline class, you both start and end with methods/classes, whereas you're getting rid of the interface when you're done).

Like you say, it should be pretty straightforward in its definition:

  1. Find all calls to the interface. Replace these with calls to the implementing class.
  2. Delete the interface.

As for the rarity of it, I suspect it's partly because interfaces are typically introduced for a specific purpose. This means pretty much the only time you're going to need to replace them with the implementing class is the sort of situation you describe: falling foul of YAGNI because an interface was introduced ready for multiple implementers, but they never materialised, so you're just left with a single concrete class using the royal "we".

I'd definitely say it's good practice to clean up with the refactoring - apart from the fact that future implementations may have different ideas of what should be included in the interface, there's also other trade-offs that interfaces have, such as indirection making things a little more complicated and time-consuming to debug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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