๐ WAYPOINT LESSON
Smells & Refactoring
โญโญ intermediateโณ 14 min read๐ Lesson 115 of 180
Long methods, primitive obsession, feature envy โ and comments that explain why instead of what.
Code smells and the refactoring reflex
Smells don't prove a bug โ they invite a look:
- Long method โ many blank lines and levels; extract methods
- Primitive obsession โ money as
int, phone asStringeverywhere; introduce tiny value types - Data clumps โ
city, street, ziptraveling together forever; make anAddressrecord - Feature envy โ a method reaching into another object's getters more than its own fields; move it there
- Shotgun surgery โ one change touches ten files; the concept needs a home
- Comment explaining WHAT โ the code should say what; comments say why (constraints, links, tradeoffs)
// before โ primitive obsession + long method
static boolean ok(String u, String p) { ... }
// after โ names and types carry the design
static boolean isPasswordValid(Password candidate, PasswordPolicy policy) { ... }
Refactoring discipline (from Module 12): small verified steps, tests green between each, behavior unchanged by construction.