Chapter 4.  Code Smells

Prev don't be afraid of buying books Next

Chapter 4. Code Smells

When you have learned to look at your words with critical detachment, you will find that rereading a piece five or six times in a row will each time bring to light fresh spots of trouble. [Barzun, 229]

Refactoring, or improving the design of existing code, requires that you know what code needs improvement. Catalogs of refactorings help you gain this knowledge, yet your situation may be different from what you see in a catalog. It's therefore necessary to learn common design problems so you can recognize them in your own code.

The most common design problems result from code that

  • Is duplicated

  • Is unclear

  • Is complicated

These criteria can certainly help you discover places in code that need improvement. On the other hand, many programmers find this list to be too vague; they don't know how to spot duplication in code that isn't outwardly the same, they aren't sure how to tell when code is clearly communicating its intent, and they don't know how to distinguish simple code from complicated code.

In their chapter "Bad Smells in Code" in Refactoring [F], Martin Fowler and Kent Beck provide additional guidance for identifying design problems. They liken design problems to smells and explain which refactorings, or combinations of refactorings, work best to eliminate odors.

Fowler and Beck's code smells target problems that occur everywhere: in methods, classes, hierarchies, packages (namespaces, modules), and entire systems. The names of their smells, such as Feature Envy, Primitive Obsession, and Speculative Generality, provide a rich and colorful vocabulary with which programmers may rapidly communicate about design problems.

I decided it would be useful to discover which of Fowler and Beck's 22 code smells are addressed by the refactorings I present in this book. While completing this task, I discovered 5 new code smells that suggest the need for pattern-directed refactorings. In all, the refactorings in this book address 12 code smells.

Table 4.1 lists the 12 smells and some refactorings to consider when you want to remove the smells. Deodorizing such smells is best done by considering the associated refactorings. The sections in this chapter discuss each of the 12 smells in turn and provide guidance for when to use the different refactorings.

Table 4.1.

Smell[a]

Refactoring

Duplicated Code (39) [F]

Form Template Method (205)

Introduce Polymorphic Creation with Factory Method (88)

Chain Constructors (340)

Replace One/Many Distinctions with Composite (224)

Extract Composite (214)

Unify Interfaces with Adapter (247)

Introduce Null Object (301)

Long Method (40) [F]

Compose Method (123)

Move Accumulation to Collecting Parameter (313)

Replace Conditional Dispatcher with Command (191)

Move Accumulation to Visitor (320)

Replace Conditional Logic with Strategy (129)

Conditional Complexity (41)

Replace Conditional Logic with Strategy (129)

Move Embellishment to Decorator (144)

Replace State-Altering Conditionals with State (166)

Introduce Null Object (301)

Primitive Obsession (41) [F]

Replace Type Code with Class (286)

Replace State-Altering Conditionals with State ( 166)

Replace Conditional Logic with Strategy (129)

Replace Implicit Tree with Composite (178)

Replace Implicit Language with Interpreter (269)

Move Embellishment to Decorator (144)

Encapsulate Composite with Builder (96)

Indecent Exposure (42)

Encapsulate Classes with Factory (80)

Solution Sprawl (43)

Move Creation Knowledge to Factory (68)

Alternative Classes with Different Interfaces (43) [F]

Unify Interfaces with Adapter (247)

Lazy Class (43) [F]

Inline Singleton (114)

Large Class (44) [F]

Replace Conditional Dispatcher with Command (191)

Replace State-Altering Conditionals with State (166)

Replace Implicit Language with Interpreter (269)

Switch Statements (44) [F]

Replace Conditional Dispatcher with Command (191)

Move Accumulation to Visitor (320)

Combinatorial Explosion (45)

Replace Implicit Language with Interpreter (269)

Oddball Solution (45)

Unify Interfaces with Adapter (247)




[a] Page numbers refer to the page of the current book where the smell is discussed further. [F] indicates that the smell is discussed in Fowler and Beck's chapter "Bad Smells in Code" in Refactoring [F].

Amazon


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

Similar book on Amazon

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