Sunday, November 16, 2008

Refactor Conditional Logic

In the book Refactoring to Patterns:
"Martin Fowler writes, "One of the most common areas of complexity in a program lies in complex conditional logic". We often find such logic in algorithms because they tend to grow, becoming more sophisticated over time. "

Conditional logic contain two kinds of statement: if...else... and

The book Refactoring presents us below refactoring techniques:

Replace Type Code with Class
Replace Type Code with Subclasses
Replace Type Code with State/Strategy

Decompose Conditional
Consolidate Conditional Expression
Consolidate Duplicate Conditional Fragments
Remove Control Flag
Replace Nested Conditional with Guard Clauses
Replace Conditional with Polymorphism
Introduce Null Object
Introduce Assertion

The book Refactoring to Patterns also demonstrate below conditional logic related refactorings:

Replace Conditional Logic with Strategy
Replace Conditional Dispatcher with Command
Move Accumulation to Visitor
Move Embellishment to Decorator
Replace State-Altering Conditionals with State
Introduce Null Object

As conditional logic is a common issue that increase a program's complexity and reduce its readability, examine these refactoring techniques become instructive.

No comments: