| I always wondered how that would work in a medium or large codebase. Is it easy to refactor? Refactoring can change the interface of classes, which is easier if they are explicit. And my main fear: how do I know I use the right object? I would tempted to add methods all the time to satisfy the target interface instead of using another object that already satisfies that interface. Not knowing explicitly whether I can use an object or not is confusing, or am I missing something obvious? Edit: Last but not least, how do I know my classes implement an interface that could require 5 or 10 methods? I had to do that in Go, and counting and searching the methods was really a waste of time, so much that I had to add comments to explain the interfaces that were implemented in every class. |
> And my main fear: how do I know I use the right object? I would tempted to add methods all the time to satisfy the target interface instead of using another object that already satisfies that interface.
Why don't you do this with explicit interfaces? It's one extra line of code beyond the method implementation, and it's a backwards-compatible change. I suspect that the main reason you don't is because you know that class Foo shouldn't be a Bar, and the same logic can easily guide you with implicit interfaces.
> Not knowing explicitly whether I can use an object or not is confusing, or am I missing something obvious?
This one I'll agree with. Implicit interfaces work well in quick code that you're writing yourself that fits in your head. When you're trying to understand a library someone else wrote, though, being able to have the IDE list all the implementations of an interface is very valuable.
Also, explicit interface tagging communicates authorial intent in a way that isn't possible with implicit interfaces.