Refactoring Workbook

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.

Категории