Code Smells

The 23 code smells from the CS5010 refactoring guide: how to spot each one, the refactoring that removes it, and when it is fine to leave it.

Bloaters

Code that has grown so large it is hard to work with.

Long Method

A method you have to scroll through hides where everything is. Learn to spot Long Method and break it into small methods with good names: name the intent.

Large Class

Every code base has one class that every change goes through. Learn to spot a Large Class, split it along its seams with Extract Class, and give each class one reason to change.

Primitive Obsession

A number without a unit is a guess. Learn to spot domain values hiding in primitives and give them small types that check once and are refused in the wrong place.

Long Parameter List

Eight arguments, two of them swapped, and the compiler was happy. Learn to ask four questions of every parameter and shrink a long signature from eight to two.

Data Clumps

Two dates passed separately, nine times, until one call swaps them and two families book the same cottage. Learn to spot data clumps and give them a name.

Object-Orientation Abusers

Object-oriented ideas applied incompletely or incorrectly.

Switch Statements

One new device, three silent bugs. Learn why the smell is the same switch repeated, how to replace it with a class per kind, and when an exhaustive switch is the right answer.

Temporary Field

A route planner whose fields are only filled while one method runs crashes when another method reads them early. Learn to spot a Temporary Field, give the job its own class, and know when a lazy cache is fine.

Refused Bequest

A subclass that refuses what it inherits still advertises it. Learn to spot Refused Bequest, why it breaks Liskov substitution, and how to fix the family tree: inherit a promise, not just code.

Alternative Classes with Different Interfaces

Email and text reminders do one job with two different method shapes, so every caller branches and the retry loop exists twice. Learn to line up the signatures, share a parent, and adapt what you can't change.

Change Preventers

One change forces many edits in many places.

Divergent Change

One Invoice class, three teams, three merge conflicts in one sprint. Learn to spot a class with many reasons to change, split it with Extract Class, and know when to leave it.

Shotgun Surgery

One small request, edits in five files. Learn to spot Shotgun Surgery, give each piece of knowledge a single owner, and tell it apart from its mirror image, Divergent Change.

Parallel Inheritance Hierarchies

An insurance app where every Policy needs a matching Pricer, until someone forgets one. Learn to spot families that grow in lockstep, merge them, and tell them apart from Bridge.

Dispensables

Things the code would be cleaner without.

Comments

A payroll comment says overtime is paid at 1.5x while the code says 1.25. Learn to replace what-comments with code that explains itself, and to keep the why-comments that are worth keeping.

Duplicate Code

A discount rule copied into three classes, a bug fix that reaches two. Learn to spot duplicate code, pick the right refactoring, and tell real duplication from look-alikes.

Lazy Class

A recipe app with a one-line formatter, a one-number holder and an empty subclass. Learn to spot classes that don't earn their keep and fold them in without losing good small classes.

Data Class

Three classes decide whether a loan is overdue, and they disagree. Learn to spot a Data Class, move behavior to where the data lives, and know when a plain data carrier is fine.

Dead Code

A new developer spends an afternoon fixing a coupon method nobody has called in two years. Learn to find dead code, delete it, and know when to check first.

Speculative Generality

A delivery app that only ships by bike carries a factory, an abstract Vehicle, a plugin registry and a drone parameter. Learn to spot code built for a guess and remove it.

Couplers

Classes tied together too tightly, or hidden behind too much delegation.

Feature Envy

A method that keeps reaching into another class's data wishes it lived somewhere else. Learn to spot Feature Envy and fix it with Move Method: put behavior next to the data it uses.

Inappropriate Intimacy

Team and Player reach into each other, and a small change to the roster breaks Player. Learn to spot Inappropriate Intimacy and fix it, and when close classes are fine.

Message Chains

One line prices a seat through five classes, and a small change breaks six screens. Learn to spot Message Chains, fix them with Hide Delegate or Move Method, and tell them from fluent calls.

Middle Man

Fourteen methods, twelve of them one-line forwards. Learn to spot a Middle Man, remove it, balance it against Hide Delegate, and tell it from Proxy, Decorator, Facade and Adapter.

Incomplete Library Class

LocalDate has no next business day, so five services copy the same loop and one forgets Sunday. Learn to centralise missing library behaviour with a Foreign Method or a Local Extension, and when to leave it.