Register to Better Coder! It's free.
Enjoy the premium features and succeed at every job interview.

BP10: Return interfaces instead of concrete types

Almost always return an interface type and return the most generic one making sense for the typical consuming code.


You never know what an interface type is hiding.
I'd disagree and say: Always return conrete Types but only accept interfaces (where appropriate, eg. public methods). This way you don't limit your users but still manage to have lose coupling.
Don't always blindly return interfaces instead of concrete types. Understand the difference and the implications, then choose depending on your requirements.

Download Better Coder application to your phone and get unlimited access to the collection of enterprise best practices.

Get it on Google Play