Today I came to a fundamental paradox of the object programming style, concrete types or interfaces.
Whats the better election for a method's return type: a concrete type or an interface?
In most cases, I tend to use concrete types as the return type for methods. because I believe that an concrete type is more flexible for further use and exposes more functionality.
The dark side of this: Coupling. The angelic one: A concrete type contains per-se the interface you would going to return initially, and extra functionality.
What's your thumb's rule?
Is there any programming principle for this?
BONUS: This is an example of what I mean ReadOnlyCollection or IEnumerable for exposing member collections?
My rules of thumb:
1) Initially, I have the method return the interface type, because its always easy to change it to the concrete type later if necessary. Harder to go back the other way.
2) Even if the method is declared to return the concrete type, I would code the callers to use the interface type whenever possible:
InterfaceType i = xyz.methodThatReturnsConcreteType();
.
3) Whether I own the calling code makes a difference too (internal vs public APIs):
Other considerations:
In summary,