Code Smells: Symptom, Cause, Cure
Every pattern in the course began as a smell in its problem story. This chapter gathers them into families, so a learner who meets a smell they've never seen can still work out what's wrong and what would fix it.
Updated
The Smell Families
- Smells of Choosing: The switch That Keeps Growing (Journeyman): When the same switch on a kind of thing is copied into many places, a new kind means a hunt through the code and one copy is always missed; the smells of choosing all come from one decision with no home, and their cures give it one.
- Smells of Making: new Everywhere (Journeyman): When the code that uses an object also builds it (new scattered through business code, a constructor with nine arguments, a class that opens its own database connection), every change to how things are made touches the code that uses them, and nothing can be tested on its own; the cures give making its own home.
- Smells of Tangling: Reaching Through Objects (Journeyman): When code reaches through one object to another to get an answer (booking.member().plan().price()), one small rule change touches every place that reaches, and one is always missed; the smells of tangling all come from code that knows too much about other objects, and their cures move the knowledge to where it belongs.
- Smells of Size: The Long Method and the Class That Does Everything (Journeyman): When one method or class keeps every job that was ever added to it, nothing inside can be reused or tested on its own, and a new feature means copying part of it; the smells of size come from jobs that were never given names, and the plainest cure, pulling out a method, gives them one.
- Smells of Combining: A Class for Every Combination (Journeyman): When every mix of features gets its own class (YogaClassWithReminderAndWaitlist), or every missing piece gets its own null check, each new feature doubles the classes or adds a check to every screen; the smells of combining come from building mixes by copying instead of from small parts, and their cures put the parts together.
The Smell Map
- The Smell Map: From Symptom to Cure (Journeyman): Every smell this course has met fits one of five families, and each family has one cause and a short list of cures; three questions (what changes together, what has no home, who knows too much) diagnose a smell you've never seen, and the smallest cure, often pulling out a method, comes first.
Exercise: The Smell Clinic
- Exercise: The Smell Clinic (Journeyman): Diagnose Summit Gym's booking code like a doctor: five smells, one from each family, each written up as symptom, cause and cure. Then cure the three that today's changes are waiting on, with the tests green throughout.