Duplicated Code

Prev don't be afraid of buying books Next

Duplicated code is the most pervasive and pungent smell in software. It tends to be either explicit or subtle. Explicit duplication exists in identical code, while subtle duplication exists in structures or processing steps that are outwardly different yet essentially the same.

You can often remove explicit and/or subtle duplication in subclasses of a hierarchy by applying Form Template Method (205). If a method in the subclasses is implemented similarly, except for an object creation step, applying Introduce Polymorphic Creation with Factory Method (88) will pave the way for removing more duplication by means of a Template Method.

If the constructors of a class contain duplicated code, you can often eliminate the duplication by applying Chain Constructors (340).

If you have separate code for processing a single object or a collection of objects, you may be able to remove duplication by applying Replace One/Many Distinctions with Composite (224).

If subclasses of a hierarchy each implement their own Composite, the implementations may be identical, in which case you can use Extract Composite (214).

If you process objects differently merely because they have different interfaces, applying Unify Interfaces with Adapter (247) will pave the way for removing duplicated processing logic.

If you have conditional logic to deal with an object when it is null and the same null logic is duplicated throughout your system, applying Introduce Null Object (301) will eliminate the duplication and simplify the system.

Amazon


Refactoring to Patterns (The Addison-Wesley Signature Series)
Refactoring to Patterns
ISBN: 0321213351
EAN: 2147483647
Year: 2003
Pages: 103

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