Skip to content

Large Class

A Large Class collects unrelated fields and responsibilities until no one can change one part without reading all of it.

7 min readCode smellBloat

A Large Class is a class that does the work of several. It usually starts small and focused, then becomes the default place for anything loosely related: pricing next to scheduling, email next to CSV export, configuration next to business rules. The cost builds slowly. Every new feature means reading hundreds of lines to find the part that matters. Tests need a full object graph to check one calculation. Two developers working on unrelated features end up editing the same file and colliding in merges. In an interview, being able to spot the hidden classes inside a large one, and to explain the order in which you would pull them out, shows that you can think about design and not just syntax.

Signs

How to spot it in a review.

  • The class has many fields, and each method uses only a small subset of them.
  • Field names cluster by prefix or theme, such as smtpHost and sentReminders, or vatRate and payoutShare.
  • You can describe what the class does only with a sentence full of "and".
  • Unit tests for one method need constructor arguments that the method never touches.
  • The file is a merge-conflict hotspot for changes that have nothing to do with each other.
  • Several methods would sit more naturally on a data object the class passes around.

What it looks like

A typical case, in three languages.

type Session = {
  id: string;
  tutorId: string;
  studentEmail: string;
  startsAt: Date;
  minutes: number;
  subject: string;
};

const DAY_MS = 24 * 60 * 60 * 1000;

function overlaps(a: Session, b: Session): boolean {
  const aEnd = a.startsAt.getTime() + a.minutes * 60_000;
  const bEnd = b.startsAt.getTime() + b.minutes * 60_000;
  return a.startsAt.getTime() < bEnd && b.startsAt.getTime() < aEnd;
}

export class TutoringDesk {
  private sessions: Session[] = [];
  private hourlyRates = new Map<string, number>();
  private payoutShare = 0.8;
  private vatRate = 0.2;
  private sentReminders = new Set<string>();
  private exportDelimiter = ",";

  constructor(private smtpHost: string) {}

  setRate(tutorId: string, perHour: number): void {
    this.hourlyRates.set(tutorId, perHour);
  }

  book(session: Session): void {
    const clash = this.sessions.some(
      (s) => s.tutorId === session.tutorId && overlaps(s, session),
    );
    if (clash) throw new Error(`Tutor ${session.tutorId} is busy`);
    this.sessions.push(session);
  }

  priceFor(session: Session): number {
    const rate = this.hourlyRates.get(session.tutorId) ?? 0;
    return ((rate * session.minutes) / 60) * (1 + this.vatRate);
  }

  tutorPayout(tutorId: string): number {
    const rate = this.hourlyRates.get(tutorId) ?? 0;
    return this.sessions
      .filter((s) => s.tutorId === tutorId)
      .reduce((sum, s) => sum + ((rate * s.minutes) / 60) * this.payoutShare, 0);
  }

  reminderEmails(now: Date): string[] {
    const messages: string[] = [];
    for (const s of this.sessions) {
      if (this.sentReminders.has(s.id)) continue;
      const start = s.startsAt.getTime();
      if (start > now.getTime() && start <= now.getTime() + DAY_MS) {
        this.sentReminders.add(s.id);
        messages.push(
          `smtp://${this.smtpHost} -> ${s.studentEmail}: ${s.subject} at ${s.startsAt.toISOString()}`,
        );
      }
    }
    return messages;
  }

  exportCsv(): string {
    const d = this.exportDelimiter;
    const rows = [["id", "tutor", "start", "minutes", "price"].join(d)];
    for (const s of this.sessions) {
      rows.push(
        [s.id, s.tutorId, s.startsAt.toISOString(), String(s.minutes), this.priceFor(s).toFixed(2)].join(d),
      );
    }
    return rows.join("\n");
  }
}

TutoringDesk belongs to a tutoring marketplace and handles four separate concerns: booking with clash detection, pricing and tutor payouts, reminder emails, and CSV export. The fields group cleanly by concern. hourlyRates, vatRate and payoutShare are used only by pricing. smtpHost and sentReminders are used only by reminders. exportDelimiter is used only by export. Changing the VAT rule means opening the same file that holds email formatting, and testing priceFor means building an object that needs an SMTP host.

Why it happens

How code ends up like this.

  • Adding a method to an existing class is faster than deciding where a new class should live, so each shortcut feels harmless at the time.
  • The class owns the central data, such as the session list, so every feature that touches that data gets pulled into it.
  • Configuration values like rates and delimiters are stored as loose fields instead of in their own objects, which invites behaviour to gather around them.
  • Nobody owns the design of the module, so no one notices when one class has become five.

Treatment

The refactorings that cure it, and when to choose each.

Payoff

What you get once it's gone.

  • Each new class has a single reason to change, so pricing changes no longer touch email code.
  • Tests for one concern need only the collaborators that concern uses.
  • Unrelated features stop colliding in the same file during merges.
  • Names such as TutorPricing and ReminderMailer document the design better than one catch-all class.

When to leave it

Sometimes it's not worth fixing.

  • A class whose many fields all serve one cohesive job, such as a configuration record or a generated data transfer object, is long but not large in the harmful sense.
  • Framework entry points like a controller with several closely related actions can stay together if each action is thin and delegates elsewhere.
  • If the class is scheduled for deletion or replacement soon, splitting it may cost more than it returns.

Common questions

Short answers to the usual doubts.

How many lines or fields make a class too large?

There is no reliable number. Look at cohesion instead: if groups of methods use disjoint sets of fields, the class holds several classes. A 400-line class with one focused job can be fine, while a 150-line class mixing billing and email is already a problem.

Where should I start when splitting a Large Class?

List the fields and mark which methods use each one. The clusters that appear are your candidate classes. Extract the cluster with the fewest connections to the rest first, keep the original class delegating to it, run the tests, then continue with the next cluster.

Is Large Class the same as a God Object?

They overlap. A God Object is an extreme Large Class that also knows about and controls much of the rest of the system. Every God Object is a Large Class, but a Large Class can be self-contained and still need splitting.

Won't extracting classes just spread the complexity around?

The total amount of logic stays the same, but each piece becomes readable and testable on its own. The original class becomes a small coordinator, and you can reason about pricing without reading email code. If an extracted class ends up doing almost nothing, inline it back.

How do I talk about this smell in an interview?

Point to concrete evidence such as field clusters and tests that need irrelevant setup, name the classes you would extract, and explain the order and how you keep behaviour unchanged with tests at each step. Interviewers care more about a safe incremental plan than a perfect final diagram.

Next guide3 of 5 · Smells: bloatLong Parameter ListRead the next one