Professional Documents
Culture Documents
Software Craftsmanship: Code Smells
Software Craftsmanship: Code Smells
Software Craftsmanship: Code Smells
Code Smells
Software Craftsmanship Manifesto
• Lost Intent
• Inefficient Name
• Duplicated Code
• Deodorant Comment
• Long Method
• Large Class
• Lazy Class
More…
Lost Intent
Just as a traffic sign may be obscured by shrubs, Code that doesn't easily communicate
it's author's intention is hazardous: hard to understand, extend and modify.
Lost Intent
Intention-Revealing Code Lost Intent
Low-level or complex logic obscures
The purpose of the code is clear.
the code's purpose.
The location of code makes little
You can easily find the code you seek.
sense.
Every step of an algorithm is clearly The steps of the algorithm are
defined. blended together.
Lost Intent
Inefficient Name
tmp = finder.fetch("0182901");
Account theAccount = finder.fetch(transaction.getSource());
account = finder.fetch( new StateMatcher("VA") );
for(Account item : finder.fetchAll()) { … }
getDrctn()
getDir()
getDirection()
Duplicated Code
If you see the same code structure in more than one place, you can be sure that your program
will be better if you find a way to unify them.
— Refactoring[page 76], Martin Fowler and Kent Beck
Duplicated Code
Deodorant Comment
WebServicesProviderContoller
• performValidationCB()
• executeBusinessProcessCB()
Lazy Class
Employee constructor
.
Passing Jobs in
Employee
constructor.
Oddball Solution
When a problem is solved one way throughout a system and the same problem is
solved another way in the same system, one of the solutions is the oddball or
inconsistent solution.
Oddball Solution
Primitive Obsession
Primitive Obsession exists when code solves a problem using a tool that's too simplistic.
Primitive Obsession
Switch Statement
Most times you see a switch statement you should consider polymorphism.
— Refactoring, Martin Fowler and Kent Beck (page 82).
Switch Statement
Move each leg of the conditional to an overriding method in a subclass. Make the original
method abstract.
Switch Statement
You get this smell when people say "Oh, I think we will need the ability to do that someday"
and thus want all sorts of hooks and special cases to handle things that aren't required. —
Refactoring, Martin Fowler and Kent Beck (page 83).
Speculative Generality
Long Parameter List
Methods that take too many parameters produce client code that is awkward and
difficult to work with.
Long Parameter List
Conditional logic is innocent in its infancy, when it is simple to understand and contained
within a few lines of code. Unfortunately, it rarely ages well.
— Joshua Kerievsky, Refactoring to Patterns, page 41.
Conditional Complexity
Combinatorial Explosion
When new combinations of data or behavior further bloat an already bloated design, you've got
a Combinatorial Explosion smell.
Combinatorial Explosion
Alternative Classes With Different
Interfaces
This subtle smell results when differences in the interfaces of similar classes leads to
duplicated code.
Alternative Classes With Different
Interfaces
Inappropriate Intimacy
Sometimes classes become far too intimate and spend too much time delving in each others'
private parts.
— Refactoring [page 85], Fowler and Beck
Inappropriate Intimacy
Indecent Exposure
Subclasses get to inherit the methods and data of their parents. But what if they don't
want or need what they are given? — Refactoring[page 87], Martin Fowler and Kent Beck
Refused Bequest
Black Sheep
Black Sheep
Solution Sprawl
When code and/or data used to perform a responsibility becomes sprawled across numerous
classes, Solution Sprawl is in the air.
— Joshua Kerievsky, Refactoring to Patterns, page 43.
Solution Sprawl
Feature Envy
Feature Envy
Temporary Field
An object's field (a.k.a. instance variable) should have meaning during the full lifetime
of the object.
Temporary Field
Side Effect
Side Effect
References