Smells of Size: The Long Method and the Class That Does Everything
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.
Journeyman · Software Development · OOP · Updated
After this lesson you can
- Spot a smell of size in code and name its cause
- Choose the cure for a smell of size: pull out a method or a class, Template Method, Strategy or Chain of Responsibility
Key takeaways
- The smells of size: a long method with sections and comments, a class that does everything (a god class), and the same steps copied with small changes.
- Their cause: each feature was added where it was easiest, so jobs that deserve their own names never got them.
- Pulling out a method, named after what its lines do, is the plainest cure there is; a long method's comments are often the names it's asking for.
- Pull out a class when one class has several jobs, and reach for Template Method or Strategy when the same steps are copied with one step different.
Before this: the first three families: Smells of Choosing, Smells of Making and Smells of Tangling.
The front desk at Summit Gym wants a new button: "Can I book?" A member asks whether there's room in tonight's spin class and what it would cost, and the desk answers without booking anything. Every check the button needs already exists. They're inside book(), the method that books a place, and in the real system it's 200 lines long.

The symptom: one method, every job
Here is book(), cut down to the part the button needs. It lives in GymManager, the class that does everything. Each class has 12 places; a basic-plan member gets 4 classes a month and then pays the drop-in price:
GymManager gym = new GymManager();
Console.WriteLine(gym.Book(new Member("Maya", "basic", 4), "Yoga"));
Console.WriteLine(gym.Book(new Member("Sam", "unlimited", 9), "Spin"));
class Member(string name, string plan, int used)
{
public string Name => name;
public string Plan => plan;
public int Used { get; set; } = used;
}
class GymManager
{
private readonly Dictionary<string, int> taken = new Dictionary<string, int> { ["Yoga"] = 11, ["Spin"] = 12 };
private readonly Dictionary<string, int> dropIn = new Dictionary<string, int> { ["Yoga"] = 15, ["Spin"] = 18 };
public string Book(Member member, string kind)
{
// Check the class has room.
int places = taken[kind];
if (places >= 12)
{
return quot;{member.Name}: {kind} is full";
}
// Work out the price.
int price = 0;
if (member.Plan == "basic" && member.Used >= 4)
{
price = dropIn[kind];
}
// Take the place.
taken[kind] = places + 1;
member.Used += 1;
// Confirm it.
return quot;{member.Name}: booked into {kind}, place {places + 1} of 12, ${price}";
}
}
Maya: booked into Yoga, place 12 of 12, $15
Sam: Spin is full
static class Member {
final String name;
final String plan;
int used;
Member(String name, String plan, int used) { this.name = name; this.plan = plan; this.used = used; }
}
static class GymManager {
private final Map<String, Integer> taken = new HashMap<>(Map.of("Yoga", 11, "Spin", 12));
private final Map<String, Integer> dropIn = Map.of("Yoga", 15, "Spin", 18);
String book(Member member, String kind) {
// Check the class has room.
int places = taken.get(kind);
if (places >= 12) {
return member.name + ": " + kind + " is full";
}
// Work out the price.
int price = 0;
if (member.plan.equals("basic") && member.used >= 4) {
price = dropIn.get(kind);
}
// Take the place.
taken.put(kind, places + 1);
member.used += 1;
// Confirm it.
return member.name + ": booked into " + kind + ", place " + (places + 1) + " of 12, quot; + price;
}
}
void main() {
GymManager gym = new GymManager();
IO.println(gym.book(new Member("Maya", "basic", 4), "Yoga"));
IO.println(gym.book(new Member("Sam", "unlimited", 9), "Spin"));
}
Maya: booked into Yoga, place 12 of 12, $15
Sam: Spin is full
class Member:
def __init__(self, name, plan, used):
self.name = name
self.plan = plan
self.used = used
class GymManager:
def __init__(self):
self.taken = {"Yoga": 11, "Spin": 12}
self.drop_in = {"Yoga": 15, "Spin": 18}
def book(self, member, kind):
# Check the class has room.
places = self.taken[kind]
if places >= 12:
return f"{member.name}: {kind} is full"
# Work out the price.
price = 0
if member.plan == "basic" and member.used >= 4:
price = self.drop_in[kind]
# Take the place.
self.taken[kind] = places + 1
member.used += 1
# Confirm it.
return f"{member.name}: booked into {kind}, place {places + 1} of 12, ${price}"
gym = GymManager()
print(gym.book(Member("Maya", "basic", 4), "Yoga"))
print(gym.book(Member("Sam", "unlimited", 9), "Spin"))
Maya: booked into Yoga, place 12 of 12, $15
Sam: Spin is full
class Member {
constructor(name, plan, used) { this.name = name; this.plan = plan; this.used = used; }
}
class GymManager {
taken = { Yoga: 11, Spin: 12 };
dropIn = { Yoga: 15, Spin: 18 };
book(member, kind) {
// Check the class has room.
const places = this.taken[kind];
if (places >= 12) {
return `${member.name}: ${kind} is full`;
}
// Work out the price.
let price = 0;
if (member.plan === "basic" && member.used >= 4) {
price = this.dropIn[kind];
}
// Take the place.
this.taken[kind] = places + 1;
member.used += 1;
// Confirm it.
return `${member.name}: booked into ${kind}, place ${places + 1} of 12, ${price}`;
}
}
const gym = new GymManager();
console.log(gym.book(new Member("Maya", "basic", 4), "Yoga"));
console.log(gym.book(new Member("Sam", "unlimited", 9), "Spin"));
Maya: booked into Yoga, place 12 of 12, $15
Sam: Spin is full
class Member {
name: string;
plan: string;
used: number;
constructor(name: string, plan: string, used: number) { this.name = name; this.plan = plan; this.used = used; }
}
class GymManager {
taken: Record<string, number> = { Yoga: 11, Spin: 12 };
dropIn: Record<string, number> = { Yoga: 15, Spin: 18 };
book(member: Member, kind: string): string {
// Check the class has room.
const places = this.taken[kind];
if (places >= 12) {
return `${member.name}: ${kind} is full`;
}
// Work out the price.
let price = 0;
if (member.plan === "basic" && member.used >= 4) {
price = this.dropIn[kind];
}
// Take the place.
this.taken[kind] = places + 1;
member.used += 1;
// Confirm it.
return `${member.name}: booked into ${kind}, place ${places + 1} of 12, ${price}`;
}
}
const gym = new GymManager();
console.log(gym.book(new Member("Maya", "basic", 4), "Yoga"));
console.log(gym.book(new Member("Sam", "unlimited", 9), "Spin"));
Maya: booked into Yoga, place 12 of 12, $15
Sam: Spin is full
It works. But the button needs "is there room?" and "what would it cost?" without "take the place". The only way to get them is to copy those lines into a second method, and then there are two copies of the price rule.
- The symptom: a long method, one method doing several jobs one after another. The comments give it away: "Check the class has room", "Work out the price", "Take the place". Each comment is the name of a job that never got its own method.
- The cause: every feature was added where it was easiest, at the bottom of the method that was already open. Nobody decided to write 200 lines; it grew a few lines at a time.
The rest of the family
- The class that does everything.
GymManageralso bills members, sends emails, prints the weekly report, plans the staff rota and resets the door codes: sixty methods, and the file everyone has open. It's called a god class, or a god object: it knows and does everything, so every change goes through it. (Single Responsibility: One Reason to Change is the principle it breaks; Anti-Patterns in the next chapter meets it again.) - The same steps, copied with small changes. The weekly report and the monthly report are two copies of the same 40 lines, with one step different. A fix in one copy doesn't reach the other.
The cure: pull out a method
What would you try? Copying the checks into canBook is the fast way, and it's how the price rule ends up in two places. A flag, book(member, kind, dryRun: true), is the smell of choosing from the first lesson of this chapter.
The cure is the plainest refactoring there is: pull out a method for each job, and name it after its comment. Books on refactoring call it Extract Method. Then book reads like its own comments, and the button reuses the pieces:
GymManager gym = new GymManager();
Console.WriteLine(gym.CanBook(new Member("Leo", "basic", 1), "Yoga"));
Console.WriteLine(gym.Book(new Member("Maya", "basic", 4), "Yoga"));
Console.WriteLine(gym.Book(new Member("Sam", "unlimited", 9), "Spin"));
class Member(string name, string plan, int used)
{
public string Name => name;
public string Plan => plan;
public int Used { get; set; } = used;
}
class GymManager
{
private readonly Dictionary<string, int> taken = new Dictionary<string, int> { ["Yoga"] = 11, ["Spin"] = 12 };
private readonly Dictionary<string, int> dropIn = new Dictionary<string, int> { ["Yoga"] = 15, ["Spin"] = 18 };
public string CanBook(Member member, string kind) =>
HasRoom(kind) ? quot;{member.Name} can book {kind}: ${PriceFor(member, kind)}" : quot;{member.Name}: {kind} is full";
public string Book(Member member, string kind)
{
if (!HasRoom(kind))
{
return quot;{member.Name}: {kind} is full";
}
int price = PriceFor(member, kind);
int place = TakePlace(member, kind);
return quot;{member.Name}: booked into {kind}, place {place} of 12, ${price}";
}
private bool HasRoom(string kind) => taken[kind] < 12;
private int PriceFor(Member member, string kind) => member.Plan == "basic" && member.Used >= 4 ? dropIn[kind] : 0;
private int TakePlace(Member member, string kind)
{
taken[kind] += 1;
member.Used += 1;
return taken[kind];
}
}
Leo can book Yoga: $0
Maya: booked into Yoga, place 12 of 12, $15
Sam: Spin is full
static class Member {
final String name;
final String plan;
int used;
Member(String name, String plan, int used) { this.name = name; this.plan = plan; this.used = used; }
}
static class GymManager {
private final Map<String, Integer> taken = new HashMap<>(Map.of("Yoga", 11, "Spin", 12));
private final Map<String, Integer> dropIn = Map.of("Yoga", 15, "Spin", 18);
String canBook(Member member, String kind) {
return hasRoom(kind) ? member.name + " can book " + kind + ": quot; + priceFor(member, kind) : member.name + ": " + kind + " is full";
}
String book(Member member, String kind) {
if (!hasRoom(kind)) {
return member.name + ": " + kind + " is full";
}
int price = priceFor(member, kind);
int place = takePlace(member, kind);
return member.name + ": booked into " + kind + ", place " + place + " of 12, quot; + price;
}
private boolean hasRoom(String kind) { return taken.get(kind) < 12; }
private int priceFor(Member member, String kind) { return member.plan.equals("basic") && member.used >= 4 ? dropIn.get(kind) : 0; }
private int takePlace(Member member, String kind) {
taken.put(kind, taken.get(kind) + 1);
member.used += 1;
return taken.get(kind);
}
}
void main() {
GymManager gym = new GymManager();
IO.println(gym.canBook(new Member("Leo", "basic", 1), "Yoga"));
IO.println(gym.book(new Member("Maya", "basic", 4), "Yoga"));
IO.println(gym.book(new Member("Sam", "unlimited", 9), "Spin"));
}
Leo can book Yoga: $0
Maya: booked into Yoga, place 12 of 12, $15
Sam: Spin is full
class Member:
def __init__(self, name, plan, used):
self.name = name
self.plan = plan
self.used = used
class GymManager:
def __init__(self):
self.taken = {"Yoga": 11, "Spin": 12}
self.drop_in = {"Yoga": 15, "Spin": 18}
def can_book(self, member, kind):
if not self.has_room(kind):
return f"{member.name}: {kind} is full"
return f"{member.name} can book {kind}: ${self.price_for(member, kind)}"
def book(self, member, kind):
if not self.has_room(kind):
return f"{member.name}: {kind} is full"
price = self.price_for(member, kind)
place = self.take_place(member, kind)
return f"{member.name}: booked into {kind}, place {place} of 12, ${price}"
def has_room(self, kind):
return self.taken[kind] < 12
def price_for(self, member, kind):
return self.drop_in[kind] if member.plan == "basic" and member.used >= 4 else 0
def take_place(self, member, kind):
self.taken[kind] += 1
member.used += 1
return self.taken[kind]
gym = GymManager()
print(gym.can_book(Member("Leo", "basic", 1), "Yoga"))
print(gym.book(Member("Maya", "basic", 4), "Yoga"))
print(gym.book(Member("Sam", "unlimited", 9), "Spin"))
Leo can book Yoga: $0
Maya: booked into Yoga, place 12 of 12, $15
Sam: Spin is full
class Member {
constructor(name, plan, used) { this.name = name; this.plan = plan; this.used = used; }
}
class GymManager {
taken = { Yoga: 11, Spin: 12 };
dropIn = { Yoga: 15, Spin: 18 };
canBook(member, kind) {
if (!this.#hasRoom(kind)) {
return `${member.name}: ${kind} is full`;
}
return `${member.name} can book ${kind}: ${this.#priceFor(member, kind)}`;
}
book(member, kind) {
if (!this.#hasRoom(kind)) {
return `${member.name}: ${kind} is full`;
}
const price = this.#priceFor(member, kind);
const place = this.#takePlace(member, kind);
return `${member.name}: booked into ${kind}, place ${place} of 12, ${price}`;
}
#hasRoom(kind) { return this.taken[kind] < 12; }
#priceFor(member, kind) { return member.plan === "basic" && member.used >= 4 ? this.dropIn[kind] : 0; }
#takePlace(member, kind) {
this.taken[kind] += 1;
member.used += 1;
return this.taken[kind];
}
}
const gym = new GymManager();
console.log(gym.canBook(new Member("Leo", "basic", 1), "Yoga"));
console.log(gym.book(new Member("Maya", "basic", 4), "Yoga"));
console.log(gym.book(new Member("Sam", "unlimited", 9), "Spin"));
Leo can book Yoga: $0
Maya: booked into Yoga, place 12 of 12, $15
Sam: Spin is full
class Member {
name: string;
plan: string;
used: number;
constructor(name: string, plan: string, used: number) { this.name = name; this.plan = plan; this.used = used; }
}
class GymManager {
taken: Record<string, number> = { Yoga: 11, Spin: 12 };
dropIn: Record<string, number> = { Yoga: 15, Spin: 18 };
canBook(member: Member, kind: string): string {
if (!this.hasRoom(kind)) {
return `${member.name}: ${kind} is full`;
}
return `${member.name} can book ${kind}: ${this.priceFor(member, kind)}`;
}
book(member: Member, kind: string): string {
if (!this.hasRoom(kind)) {
return `${member.name}: ${kind} is full`;
}
const price = this.priceFor(member, kind);
const place = this.takePlace(member, kind);
return `${member.name}: booked into ${kind}, place ${place} of 12, ${price}`;
}
private hasRoom(kind: string): boolean { return this.taken[kind] < 12; }
private priceFor(member: Member, kind: string): number { return member.plan === "basic" && member.used >= 4 ? this.dropIn[kind] : 0; }
private takePlace(member: Member, kind: string): number {
this.taken[kind] += 1;
member.used += 1;
return this.taken[kind];
}
}
const gym = new GymManager();
console.log(gym.canBook(new Member("Leo", "basic", 1), "Yoga"));
console.log(gym.book(new Member("Maya", "basic", 4), "Yoga"));
console.log(gym.book(new Member("Sam", "unlimited", 9), "Spin"));
Leo can book Yoga: $0
Maya: booked into Yoga, place 12 of 12, $15
Sam: Spin is full
Maya's and Sam's lines are exactly what they were: the behavior didn't change, only the shape. And the new button is three lines, because "is there room?" and "what would it cost?" have names now. The price rule still exists once.
The comments are gone because the method names say the same thing. That's a good test for any comment inside a method: if it names what the next lines do, it's asking to become a method.
For the god class, the cure is the same move one size up: pull out a class for each job (Billing, Emails, WeeklyReport), and let GymManager shrink until it has one reason to change, or disappears.
Which cure fits?
| What's too big | The cure | Where you met it |
|---|---|---|
| A method with sections, often marked by comments | Pull out a method per section, named after it | this lesson |
| A class with several jobs | Pull out a class per job | Single Responsibility: One Reason to Change |
| A page that loads data, shows it and handles clicks | Split it into model, view and controller | MVC: Model, View, Controller |
| The same steps, copied with one step different | Template Method, or Strategy for the step that differs | Template Method: Same Routine, Your Own Step; Strategy or Template Method? |
| A long row of checks, each deciding whether to go on | Chain of Responsibility | Chain of Responsibility: The Coin Sorter |
| The same data stored in thousands of objects | Flyweight | Flyweight: Share What's the Same |

Check yourself
2 questions to try on the lesson page.
So what
A comment that names the next few lines, a method you scroll through, a class everyone has open: each one is a job without a name. Give it one by pulling out a method or a class, and the next feature reuses it instead of copying it.