Thursday, October 02, 2008

Refactoring (Part 3)

  • Decompose/Consolidate Conditional
    You have a complicated conditional (if-then-else) statement - Extract methods from the condition : if and else parts
  • Consolidate Duplicate Conditional Fragments
    The same fragment of code is in all branches of a conditional expression - Move it outside of the expression.
  • Remove Control Flag
    You have a variable that is acting as a control flag for a series of boolean expressions - Use a break or return instead.
  • Replace Nested Conditional with Guard Clauses
    A method has conditional behavior that does not make clear the normal path of execution - Use guard clauses for all the special cases : The guard clause either returns, or throws an exception.
  • Replace Conditional with Polymorphism
    You have a conditional that chooses different behavior depending on the type of an object - Move each leg of the conditional to an overriding method in a subclass. Make the original method abstract.
  • Introduce Null Object
    You have repeated check for a null value - Replace the null value with a null object.
  • Introduce Assertion
    Make the assumption explicit with an assertion.

No comments: