Smells of Choosing: The switch That Keeps Growing
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.
Journeyman · Software Development · OOP · Updated
After this lesson you can
- Spot a smell of choosing in code and name the decision underneath it
- Choose the cure for a smell of choosing: polymorphism, a dictionary, Strategy, State, a dictionary of commands, or two methods
Key takeaways
- The smells of choosing: the same switch on a kind in many places, a true/false argument that changes what a method does, and "what kind are you?" checks before acting.
- Their cause is one decision ("what kind is this?") with no home of its own, so every place that needs the answer makes the decision again.
- The cure gives the decision one home: polymorphism when kinds behave differently, a dictionary when they differ only in data, two methods for a flag.
- Strategy, State and a dictionary of commands are the same cure for a rule someone picks, a status that changes, and an action chosen by name.
Before this: Code Smells: Symptom, Cause, Cure, the Module 1 lesson that reads a smell as a doctor would, and Polymorphism: One Call, Many Behaviors.
Summit Gym's class-booking system is a year old. It was written in a hurry, nobody who wrote it still works at the gym, and the business runs on it: every yoga, spin and HIIT class is booked through it. Now the owner wants to add pilates next month, and you're the one opening the code.
In Module 1 you read three smells in a food truck's code. Since then, every pattern in this course began as a smell in its problem story. This chapter gathers those smells into five families, so that when you meet one you've never seen, you can still work out what's wrong and what would fix it. The first family is the one you'll meet most: smells of choosing.

The symptom: the same switch, in five places
Every class the gym runs has a length, a room and a drop-in price (what someone without a plan pays). Each of those is decided with a switch on the kind of class. The real system has five copies of this decision, in five files. Here are three of them, with pilates added:
string[] kinds = { "Yoga", "Spin", "HIIT", "Pilates" };
foreach (string kind in kinds)
{
Console.WriteLine( quot;{kind}: {Minutes(kind)} minutes, {Room(kind)}, ${DropInPrice(kind)} drop-in");
}
// The booking screen's copy of the decision.
int Minutes(string kind) => kind switch
{
"Yoga" => 60,
"Spin" => 45,
"HIIT" => 30,
"Pilates" => 50,
_ => 0,
};
// The front desk's copy.
string Room(string kind) => kind switch
{
"Yoga" => "Studio A",
"Spin" => "Bike room",
"HIIT" => "Main floor",
"Pilates" => "Studio A",
_ => "no room",
};
// The billing code's copy, in a file nobody opened.
int DropInPrice(string kind) => kind switch
{
"Yoga" => 15,
"Spin" => 18,
"HIIT" => 12,
_ => 0,
};
Yoga: 60 minutes, Studio A, $15 drop-in
Spin: 45 minutes, Bike room, $18 drop-in
HIIT: 30 minutes, Main floor, $12 drop-in
Pilates: 50 minutes, Studio A, $0 drop-in
// The booking screen's copy of the decision.
int minutes(String kind) {
return switch (kind) {
case "Yoga" -> 60;
case "Spin" -> 45;
case "HIIT" -> 30;
case "Pilates" -> 50;
default -> 0;
};
}
// The front desk's copy.
String room(String kind) {
return switch (kind) {
case "Yoga" -> "Studio A";
case "Spin" -> "Bike room";
case "HIIT" -> "Main floor";
case "Pilates" -> "Studio A";
default -> "no room";
};
}
// The billing code's copy, in a file nobody opened.
int dropInPrice(String kind) {
return switch (kind) {
case "Yoga" -> 15;
case "Spin" -> 18;
case "HIIT" -> 12;
default -> 0;
};
}
void main() {
for (String kind : List.of("Yoga", "Spin", "HIIT", "Pilates")) {
IO.println(kind + ": " + minutes(kind) + " minutes, " + room(kind) + ", quot; + dropInPrice(kind) + " drop-in");
}
}
Yoga: 60 minutes, Studio A, $15 drop-in
Spin: 45 minutes, Bike room, $18 drop-in
HIIT: 30 minutes, Main floor, $12 drop-in
Pilates: 50 minutes, Studio A, $0 drop-in
def minutes(kind):
"""The booking screen's copy of the decision."""
match kind:
case "Yoga":
return 60
case "Spin":
return 45
case "HIIT":
return 30
case "Pilates":
return 50
return 0
def room(kind):
"""The front desk's copy."""
match kind:
case "Yoga":
return "Studio A"
case "Spin":
return "Bike room"
case "HIIT":
return "Main floor"
case "Pilates":
return "Studio A"
return "no room"
def drop_in_price(kind):
"""The billing code's copy, in a file nobody opened."""
match kind:
case "Yoga":
return 15
case "Spin":
return 18
case "HIIT":
return 12
return 0
for kind in ["Yoga", "Spin", "HIIT", "Pilates"]:
print(f"{kind}: {minutes(kind)} minutes, {room(kind)}, ${drop_in_price(kind)} drop-in")
Yoga: 60 minutes, Studio A, $15 drop-in
Spin: 45 minutes, Bike room, $18 drop-in
HIIT: 30 minutes, Main floor, $12 drop-in
Pilates: 50 minutes, Studio A, $0 drop-in
// The booking screen's copy of the decision.
function minutes(kind) {
switch (kind) {
case "Yoga": return 60;
case "Spin": return 45;
case "HIIT": return 30;
case "Pilates": return 50;
default: return 0;
}
}
// The front desk's copy.
function room(kind) {
switch (kind) {
case "Yoga": return "Studio A";
case "Spin": return "Bike room";
case "HIIT": return "Main floor";
case "Pilates": return "Studio A";
default: return "no room";
}
}
// The billing code's copy, in a file nobody opened.
function dropInPrice(kind) {
switch (kind) {
case "Yoga": return 15;
case "Spin": return 18;
case "HIIT": return 12;
default: return 0;
}
}
for (const kind of ["Yoga", "Spin", "HIIT", "Pilates"]) {
console.log(`${kind}: ${minutes(kind)} minutes, ${room(kind)}, ${dropInPrice(kind)} drop-in`);
}
Yoga: 60 minutes, Studio A, $15 drop-in
Spin: 45 minutes, Bike room, $18 drop-in
HIIT: 30 minutes, Main floor, $12 drop-in
Pilates: 50 minutes, Studio A, $0 drop-in
// The booking screen's copy of the decision.
function minutes(kind: string): number {
switch (kind) {
case "Yoga": return 60;
case "Spin": return 45;
case "HIIT": return 30;
case "Pilates": return 50;
default: return 0;
}
}
// The front desk's copy.
function room(kind: string): string {
switch (kind) {
case "Yoga": return "Studio A";
case "Spin": return "Bike room";
case "HIIT": return "Main floor";
case "Pilates": return "Studio A";
default: return "no room";
}
}
// The billing code's copy, in a file nobody opened.
function dropInPrice(kind: string): number {
switch (kind) {
case "Yoga": return 15;
case "Spin": return 18;
case "HIIT": return 12;
default: return 0;
}
}
for (const kind of ["Yoga", "Spin", "HIIT", "Pilates"]) {
console.log(`${kind}: ${minutes(kind)} minutes, ${room(kind)}, ${dropInPrice(kind)} drop-in`);
}
Yoga: 60 minutes, Studio A, $15 drop-in
Spin: 45 minutes, Bike room, $18 drop-in
HIIT: 30 minutes, Main floor, $12 drop-in
Pilates: 50 minutes, Studio A, $0 drop-in
Pilates is free to drop in on. Nobody decided that: the person who added pilates found two copies of the switch and didn't know about the third, and its default quietly answered 0. With five copies in five files, missing one is the normal outcome, not a careless one.
Read it as a doctor would:
- The symptom: the same
switchon the kind of class, in many places. Books on refactoring call this smell repeated switches. - The cause: the decision "what kind of class is this, and what does that mean?" has no home of its own, so every place that needs an answer makes the decision again, and every copy has to change together.
- The cure: give the decision one home.
The rest of the family
Two more smells come from the same cause, and you'll see them in the same code:
- A flag argument. The gym's code calls
book(maya, yoga, false)in one place andbook(sam, yoga, true)in another. They look alike, but only readingbooktells you thattruemeans "don't book a place: put him on the waitlist instead". A true/false argument that switches a method between two jobs hides a decision inside the method, and every reader has to look it up. - "What kind are you?" checks.
if (booking.kind == "HIIT") { remindToBringWater(); }, somewhere far from the class itself. The code asks an object what it is, and then decides for it. Every new kind means finding every such check.
Each of them hides the same thing: a choice that belongs in one place has been copied into many.
The cure: let each kind answer for itself
What would you try first? Adding "Pilates" => 16 to the billing copy fixes today's bug, and leaves the next kind to the same hunt. A default that stops with an error ("no price for Pilates") is a fine first aid: the gap now shouts instead of charging $0. But it still needs five edits.
The cure moves the decision into the kinds themselves. Each kind of class is a small class that knows its own length, room and price, and the code that prints a line asks it. That's polymorphism: one call, kind.dropInPrice, and each kind answers in its own way.
List<IClassKind> kinds = new List<IClassKind> { new Yoga(), new Spin(), new Hiit(), new Pilates() };
foreach (IClassKind kind in kinds)
{
Console.WriteLine( quot;{kind.Name}: {kind.Minutes} minutes, {kind.Room}, ${kind.DropInPrice} drop-in");
}
interface IClassKind
{
string Name { get; }
int Minutes { get; }
string Room { get; }
int DropInPrice { get; }
}
class Yoga : IClassKind { public string Name => "Yoga"; public int Minutes => 60; public string Room => "Studio A"; public int DropInPrice => 15; }
class Spin : IClassKind { public string Name => "Spin"; public int Minutes => 45; public string Room => "Bike room"; public int DropInPrice => 18; }
class Hiit : IClassKind { public string Name => "HIIT"; public int Minutes => 30; public string Room => "Main floor"; public int DropInPrice => 12; }
class Pilates : IClassKind { public string Name => "Pilates"; public int Minutes => 50; public string Room => "Studio A"; public int DropInPrice => 16; }
Yoga: 60 minutes, Studio A, $15 drop-in
Spin: 45 minutes, Bike room, $18 drop-in
HIIT: 30 minutes, Main floor, $12 drop-in
Pilates: 50 minutes, Studio A, $16 drop-in
interface ClassKind {
String name();
int minutes();
String room();
int dropInPrice();
}
class Yoga implements ClassKind { public String name() { return "Yoga"; } public int minutes() { return 60; } public String room() { return "Studio A"; } public int dropInPrice() { return 15; } }
class Spin implements ClassKind { public String name() { return "Spin"; } public int minutes() { return 45; } public String room() { return "Bike room"; } public int dropInPrice() { return 18; } }
class Hiit implements ClassKind { public String name() { return "HIIT"; } public int minutes() { return 30; } public String room() { return "Main floor"; } public int dropInPrice() { return 12; } }
class Pilates implements ClassKind { public String name() { return "Pilates"; } public int minutes() { return 50; } public String room() { return "Studio A"; } public int dropInPrice() { return 16; } }
void main() {
for (ClassKind kind : List.of(new Yoga(), new Spin(), new Hiit(), new Pilates())) {
IO.println(kind.name() + ": " + kind.minutes() + " minutes, " + kind.room() + ", quot; + kind.dropInPrice() + " drop-in");
}
}
Yoga: 60 minutes, Studio A, $15 drop-in
Spin: 45 minutes, Bike room, $18 drop-in
HIIT: 30 minutes, Main floor, $12 drop-in
Pilates: 50 minutes, Studio A, $16 drop-in
class Yoga:
name = "Yoga"
minutes = 60
room = "Studio A"
drop_in_price = 15
class Spin:
name = "Spin"
minutes = 45
room = "Bike room"
drop_in_price = 18
class Hiit:
name = "HIIT"
minutes = 30
room = "Main floor"
drop_in_price = 12
class Pilates:
name = "Pilates"
minutes = 50
room = "Studio A"
drop_in_price = 16
for kind in [Yoga(), Spin(), Hiit(), Pilates()]:
print(f"{kind.name}: {kind.minutes} minutes, {kind.room}, ${kind.drop_in_price} drop-in")
Yoga: 60 minutes, Studio A, $15 drop-in
Spin: 45 minutes, Bike room, $18 drop-in
HIIT: 30 minutes, Main floor, $12 drop-in
Pilates: 50 minutes, Studio A, $16 drop-in
class Yoga { name = "Yoga"; minutes = 60; room = "Studio A"; dropInPrice = 15; }
class Spin { name = "Spin"; minutes = 45; room = "Bike room"; dropInPrice = 18; }
class Hiit { name = "HIIT"; minutes = 30; room = "Main floor"; dropInPrice = 12; }
class Pilates { name = "Pilates"; minutes = 50; room = "Studio A"; dropInPrice = 16; }
for (const kind of [new Yoga(), new Spin(), new Hiit(), new Pilates()]) {
console.log(`${kind.name}: ${kind.minutes} minutes, ${kind.room}, ${kind.dropInPrice} drop-in`);
}
Yoga: 60 minutes, Studio A, $15 drop-in
Spin: 45 minutes, Bike room, $18 drop-in
HIIT: 30 minutes, Main floor, $12 drop-in
Pilates: 50 minutes, Studio A, $16 drop-in
interface ClassKind {
name: string;
minutes: number;
room: string;
dropInPrice: number;
}
class Yoga implements ClassKind { name = "Yoga"; minutes = 60; room = "Studio A"; dropInPrice = 15; }
class Spin implements ClassKind { name = "Spin"; minutes = 45; room = "Bike room"; dropInPrice = 18; }
class Hiit implements ClassKind { name = "HIIT"; minutes = 30; room = "Main floor"; dropInPrice = 12; }
class Pilates implements ClassKind { name = "Pilates"; minutes = 50; room = "Studio A"; dropInPrice = 16; }
const kinds: ClassKind[] = [new Yoga(), new Spin(), new Hiit(), new Pilates()];
for (const kind of kinds) {
console.log(`${kind.name}: ${kind.minutes} minutes, ${kind.room}, ${kind.dropInPrice} drop-in`);
}
Yoga: 60 minutes, Studio A, $15 drop-in
Spin: 45 minutes, Bike room, $18 drop-in
HIIT: 30 minutes, Main floor, $12 drop-in
Pilates: 50 minutes, Studio A, $16 drop-in
Python and JavaScript need no interface here: any object with a name, minutes, room and drop-in price will do (duck typing, from Interfaces: A Promise Any Class Can Keep).
Everything about pilates now lives in one line, and so does everything about each other kind. Next month's barre class is one new class, and no switch anywhere has to hear about it.
Be honest about this example, though: these kinds differ only in data, four values each. A dictionary from each kind's name to a small record of its values would cure the smell as well, with less code. A class per kind earns its place when kinds behave differently, say, when spin starts assigning each member a bike.
Which cure fits?
Every smell of choosing has the same cure, "give the decision one home", but the home depends on what the switch was choosing. You've met every one of these already:
| What the switch decides | The cure | Where you met it |
|---|---|---|
| Only data that differs by kind (a length, a price) | A dictionary from the kind to its values | this lesson |
| How each kind behaves | Polymorphism: a class per kind | this lesson |
| Which class to make, from a word | A factory, or a dictionary of functions | Simple Factory: One Place That Says new |
| Which rule applies (a deal, a carrier's rate) | Strategy: hand the rule in | Weekly Deals: Pass the Rule In; Strategy: When the Rule Needs a Class of Its Own |
| What's allowed in the current status | State: a class per status | State: What You Can Do Depends on Where It Is |
| Which action to run, picked by name | A dictionary of commands | Command: An Action You Can Hold |
| Which of two jobs a method does (a flag) | Two methods, each named for its job | this lesson: book and addToWaitlist |
The last row is the reminder this chapter keeps coming back to: not every cure is a pattern. Splitting a flagged method in two is a small refactoring, and it's often the whole fix.

Check yourself
2 questions to try on the lesson page.
So what
When you see the same switch on a kind in two places, or a true that changes what a method does, you've found a decision with no home. Give it one, and the next kind of class, rule or status is one new piece instead of a hunt.