in reply to "if" Considered Harmful in OO programming
If you find yourself using OOinheritance for polymorphism, and then having to insert if checks to correct that polymorphism, you have a problem. Clearly the polymorphism is not really doing what you want it to do, and as your object hierarchy spreads out, it will need ever more correction.
A good general rule is presented in Code Complete 2 (p 149, section 6.3):
This section has presented numerous rules for staying out of trouble with inheritance. The underlying message of all these rules is that inheritance tends to work against the primary technical imperative you have as a programmer, which is to manage complexity. For the sake of controlling complexity, you should maintain a heavy bias against inheritance.(Emphasis in original.)
Therefore only inherit when it obviously is right to so, and view with extreme doubt anything that increases the complexity of maintaining that inheritance. If it isn't perfect, don't do it.
Update: Modified per chromatic's note
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: "if" Considered Harmful in OO programming
by chromatic (Archbishop) on Sep 20, 2004 at 18:10 UTC |