Special Case


Symptoms

  • Complex if statements

  • Checks for particular values before doing work ( especially comparisons to constants or enumerations)

Causes

Someone realizes a special case is needed.

What to Do

  • If the conditionals are taking the place of polymorphism, Replace Conditional with Polymorphism .

  • If the if and then clauses are similar enough, you may be able to rewrite them so that the same code fragment can generate the proper results for each case; then the conditional can be eliminated.

Payoff

Improves communication. May expose duplication.

Contraindications

  • In a recursive definition, there is always a base case that will stop the recursion; you can't expect to eliminate these.

  • Sometimes an if clause is just the simplest way to do something.



Refactoring Workbook
Refactoring Workbook
ISBN: 0321109295
EAN: 2147483647
Year: 2003
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net