Not Using Polymorphism

An AntiPattern.

Using duplicate sets of 'if' (or switch/case) statements instead. Any time you encounter a bunch of if statements on a variable or object, then a parallel set somewhere else, then that is most likely a good candidate for polymorphism. If you have a duplicate list of if/case statements, then you have to visit two different spots to add a new one or delete an existing one. Good code does contain some interfaces and inheritance, but 'if' commands occur much more often.

The caveat is that turning the cases of the 'if' statements into subclasses may require multiple visits to add or delete polymorphic operations (if each leaf must implement its own). It is controversial whether new operations or new variations (subtypes) are a more common need.

See SwitchStatementsSmell and PolymorphismVsSelectionIdiom. Also, the TabliZer website has obsessed on this issue:

http://geocities.com/tablizer/meyer1.htm#singlechoice

http://geocities.com/tablizer/shapes.htm

http://geocities.com/tablizer/inher.htm

NoteAboutGeocities


EditText of this page (last edited August 30, 2014) or FindPage with title or text search