7

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.

2
  • 1
    en.wikipedia.org/wiki/… would seem to be close to this but not an exact match.
    – JB King
    Commented Oct 14, 2011 at 21:59
  • 1
    @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
    Commented Oct 14, 2011 at 23:20

2 Answers 2

9

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.

2
  • Thanks! Didn't know about that one, it seems to apply here, although it seems very focused on network protocols. Commented Oct 15, 2011 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
    Commented Oct 20, 2011 at 23:34
7

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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