Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I think I remember reading somewhere that one of the principles of good OO design is to write methods which take the least derived type possible, but return the most derived type possible? First, is there such a principle and second, what name would it go by? I'm looking for the name so that when I mentor other developers I can refer them to it.

share|improve this question
1  
en.wikipedia.org/wiki/… would seem to be close to this but not an exact match. –  JB King Oct 14 '11 at 21:59
    
@JB King: Co- and contra-variance deal with restrictions you have when overriding methods in a subclass. A method in a subclass cannot receive more specific type as input and cannot return a less specific type (without violating the contracts specified by the superclass and thus the substitution principle). The OP is mentioning a more general rule of thumb that isn't specific to inheritance relationships. –  hugomg Oct 14 '11 at 23:20

2 Answers 2

up vote 8 down vote accepted

This seems to be a variant of Postel's Law, also called the Robustness Principle:

Be liberal in what you expect and coservative in what you send.

share|improve this answer
    
Thanks! Didn't know about that one, it seems to apply here, although it seems very focused on network protocols. –  Steven Jeuris Oct 15 '11 at 12:45
    
I'm accepting this as there's a name, and I can say its similar to Postel's Law. I'm still willing to accept a better answer if one comes along! –  Andy Oct 20 '11 at 23:34

I don't remember the "name" for this principle but it certainly exists and is much more general than only OO.

The basic idea is to be lenient on what inputs you accept (so you can accept input from more sources) and stringent on what outputs you produce (so others have to go through less trouble to use it)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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