by oweiler · 5y ago · 0 comments
Other than that I agree, most "best practices" are subjective and boil down to personal preference.
1. They are (usually) concise specification/contract for the implementation to follow, and for the client to use.
2. They permit substitution.
These are, from my experience, the principle and most useful reasons to make use of "redundant" interface definitions. What's worse is when people want alternative implementations but only have a concrete instance, they subclass it and break all kinds of contracts making their square peg fit a round hole.
This is less useful when you have ducktyping and consequently every lookalike is considered substitutable for each other.
For example, you might want to pass around a class and only expose the getters and not the setters.
Depends on the language.
And sometimes you want to enforce a public contract of any implementation (current and future).
Personally for me, the biggest gain I see when writing against interfaces is that any class which expects a interface is already prepared for test-doubles, as opposed to classes written against concrete implementations which may even be initialised internally. Then just even starting to write a test becomes a so much bigger task.
Of course, everything with moderation, sometimes even moderation itself ;)