Code Smells and Refactoring

Beginner · 14 min

What a code smell actually is — a symptom, not a bug — the classic named smells, and refactoring as disciplined, test-covered structural repair.

Before thisComposition Over Inheritance

Modern Software Engineering · 14 min

Code Smells and Refactoring

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Modern Software EngineeringReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A code smell isn't a bug — the code runs and produces the right answer — it's a surface signal that the design underneath is going to make the next change harder than it needs to be.
  • Long Method is a function that's grown past doing one thing; the fix is Extract Method, pulling a cohesive chunk out under its own name so the original reads like a summary instead of a transcript.
  • Large Class (sometimes called a God Object) is a class that's absorbed so many unrelated jobs that almost every change to the codebase touches it, which is the Single Responsibility Principle's warning sign showing up in practice.
  • Duplicate Code means the same logic now exists in two places that have to be remembered and changed together — and the moment someone fixes a bug in one copy and forgets the other, the duplication becomes a real defect.
  • Feature Envy is a method that spends more time reaching into another object's data than using its own, which usually means the method is living in the wrong class.
  • Shotgun Surgery is when a single, focused change — add a field, rename a concept — forces edits across a dozen scattered files, a sign that one responsibility has been smeared across the codebase instead of concentrated in one place.

A smell is a symptom, not a diagnosis

A doctor doesn’t treat a fever — a fever is the sign that points at what actually needs treating. A code smell works the same way. The code compiles, the tests pass, the feature works. What a smell flags is that the design underneath that working code is going to make the next change cost more than it should. That distinction matters because it changes what you do about it: a bug gets fixed immediately because it’s breaking something right now; a smell gets scheduled, because the cost it’s warning about is future cost, not present cost. Martin Fowler and Kent Beck popularized the term in the book Refactoring, as a vocabulary for exactly this: structural warning signs that are easy to point at and name, even before you’ve worked out the specific fix.

The classic smells, and what each one is actually warning about

Long Method is a function that’s grown past doing one thing — fifteen lines turned into a hundred and fifty, because each new requirement got bolted onto the end instead of broken out. The warning it carries is that nobody can hold the whole thing in their head anymore, which is exactly when bugs slip in during edits. The standard fix is Extract Method: find a cohesive chunk, give it a name, and let the original method become a short list of calls to well-named pieces.

Large Class, sometimes called a God Object, is the same problem at a bigger scale — a class that quietly accumulated unrelated responsibilities (parsing input, writing to a database, formatting a report) until nearly every change in the codebase has a reason to touch it. It’s the Single Responsibility Principle’s violation made concrete: too many reasons for one class to change.

Duplicate Code is the same logic living in two or more places. The danger isn’t the extra typing, it’s that the copies now have to be remembered and updated together — and the first time someone fixes one copy and misses the other, the duplication has produced a real bug, not just an aesthetic complaint.

Feature Envy is a method that reaches into another object’s data more than it uses its own — a shippingCalculator that constantly pulls fields out of an Order object is, in effect, confessing that it should probably be a method on Order instead.

Shotgun Surgery is the inverse shape of a problem: one small conceptual change — renaming a status field, adding a new discount rule — forces edits scattered across a dozen files. That’s a sign the responsibility for that one concept was never consolidated anywhere; it was smeared across the codebase instead.

Refactoring: the discipline, not the vibe

Refactoring has a specific, narrow meaning: restructuring code without changing what it does. Not “cleaning up” in a loose sense, and never a rewrite — a rewrite throws away code that’s already known to work, replacing proven behavior with unproven behavior and betting the whole thing comes out equivalent. Refactoring instead moves in small, reversible steps: extract this method, rename that variable, inline this one-line wrapper — each step tiny enough that if something breaks, you know exactly which step did it.

Why tests have to come first

None of this works without a safety net. The whole point of refactoring is “the behavior didn’t change” — and the only way to actually know that, rather than hope it, is to have tests that exercise the behavior before you start, and run them again after every step. Skip the tests and refactoring quietly turns into the thing it’s supposed to not be: an unverified rewrite wearing a more reassuring name.

When a smell isn’t worth fixing

Not every smell is worth chasing immediately. A long method in code that’s stable, rarely touched, and well understood by the team costs less to leave alone than it does to refactor. The judgment call this lesson is building toward is knowing which smells sit on code that’s about to change — new features landing nearby, a team about to onboard someone unfamiliar with it — because that’s where the future cost the smell is warning about is about to come due.

Check yourself

  1. 01Why is a code smell worth fixing if the code already passes every test and works correctly in production?

    A smell isn't about current correctness, it's about future cost — the structure that produces a long method or a duplicated block makes the next bug fix or feature slower and riskier to add, even though today's behavior is fine. Ignoring the smell doesn't break anything today; it just makes tomorrow's change more expensive.

  2. 02What's the difference between Feature Envy and Shotgun Surgery, since both involve code reaching across class boundaries?

    Feature Envy is one method pulling too hard on one other class's data — the fix is moving that method closer to the data it actually uses. Shotgun Surgery is the opposite shape, where one conceptual change forces edits scattered across many classes — the fix is consolidating that responsibility into one place so future changes to it touch one file, not a dozen.

  3. 03Why does refactoring require the code to already be covered by tests, and why must it happen in small steps?

    Refactoring's entire premise is changing structure without changing behavior — and the only way to know behavior hasn't changed is to run tests before and after every step. Small steps matter because a tiny change that breaks a test points straight at its cause, while a sprawling rewrite that breaks a test leaves you guessing which of a hundred edits is responsible — which is also why refactoring is never "just rewrite it," since a rewrite throws away the known-working code the tests were protecting in the first place.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark