Beware type switches

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
(Last few edits for brief initial description)
Line 3: Line 3:
 
Type switches involve changing behaviour based on the object's type. Problems occur when multiple switches occur over the same variable in the same class, since when one is modified, all occurrences may need to be changed.  
 
Type switches involve changing behaviour based on the object's type. Problems occur when multiple switches occur over the same variable in the same class, since when one is modified, all occurrences may need to be changed.  
  
A possible solution is to replace any type code with a type class or Strategy pattern. See Martin Fowler's example at [[http://www.refactoring.com/catalog/replaceTypeCodeWithStateStrategy.html Replace Type Code With State/Strategy]]
+
A possible solution is to replace any type code with a type class or Strategy pattern. See [[Martin Fowler]]'s example at [[http://www.refactoring.com/catalog/replaceTypeCodeWithStateStrategy.html Replace Type Code With State/Strategy]]
  
 
== See also ==
 
== See also ==
 
* [[Switch statements smell]]
 
* [[Switch statements smell]]
 
* [[Riel's heuristics]]
 
* [[Riel's heuristics]]

Revision as of 01:11, 22 July 2008

This is Reil's heuristic #5.12

Type switches involve changing behaviour based on the object's type. Problems occur when multiple switches occur over the same variable in the same class, since when one is modified, all occurrences may need to be changed.

A possible solution is to replace any type code with a type class or Strategy pattern. See Martin Fowler's example at [Replace Type Code With State/Strategy]

See also

Personal tools