Skip to content

Lazy Class: When a Class Doesn't Earn Its Place

How to spot a lazy class that wraps a single line or forwards one call, why it appears, and when to inline it or keep it.

6 min readCode smellDead weight

A lazy class is a class that does so little you could delete it and lose nothing except a file, an import and a constructor call. It usually holds one field and one method. That method is often a one-liner the caller could have written itself. Every class costs something. A reader has to open it, learn its name, check its constructor and confirm it hides nothing surprising. A class that pays that back with real behaviour, a meaningful invariant or a useful seam is worth it. A lazy class charges the fee and gives nothing back. In interviews this smell comes up when you review a pull request or discuss over-engineering. You should be able to say why a class is too thin and which refactoring removes it. You should also know when a small class is exactly right.

Signs

How to spot it in a review.

  • The class has one public method, and that method is one or two lines that call a standard library function.
  • Only one caller ever instantiates it, and that caller always passes the same arguments or none.
  • Its name ends in Helper, Manager, Holder, Wrapper or Generator, but it holds no state worth protecting.
  • The tests for the class restate its single line instead of checking behaviour that could break.
  • It was once larger, but features were moved out or deleted and the shell stayed behind.
  • Deleting it and pasting its body into the caller makes the caller no harder to read.

What it looks like

A typical case, in three languages.

import { randomInt } from "node:crypto";

export class PickupCodeGenerator {
  constructor(private readonly length: number = 6) {}

  generate(): string {
    return randomInt(0, 10 ** this.length).toString().padStart(this.length, "0");
  }
}

export class HoldPeriod {
  constructor(private readonly hours: number = 72) {}

  expiresFrom(now: Date): Date {
    return new Date(now.getTime() + this.hours * 3_600_000);
  }
}

export interface Reservation {
  lockerId: string;
  parcelId: string;
  pickupCode: string;
  expiresAt: Date;
}

export class LockerReservationService {
  private readonly codes = new PickupCodeGenerator();
  private readonly hold: HoldPeriod;

  constructor(holdHours: number = 72) {
    this.hold = new HoldPeriod(holdHours);
  }

  reserve(lockerId: string, parcelId: string, now: Date = new Date()): Reservation {
    return {
      lockerId,
      parcelId,
      pickupCode: this.codes.generate(),
      expiresAt: this.hold.expiresFrom(now),
    };
  }
}

This is the reservation code for BoxRelay, a parcel-locker network. PickupCodeGenerator used to append a Luhn check digit, but that was removed when the locker firmware stopped validating it. HoldPeriod was added for per-carrier hold rules that were never built. Each class now wraps one expression, and LockerReservationService is the only caller. Use inline-class on both: move generate and expiresFrom into the service as private methods, or inline them straight into reserve. You get one class instead of three, and nothing about the behaviour changes.

Why it happens

How code ends up like this.

  • A class lost its responsibilities during earlier refactorings, and nobody deleted the empty shell.
  • It was created for a feature or variation that was planned but never shipped, which overlaps with speculative generality.
  • A rule such as one class per concept or never call the standard library directly was applied mechanically.
  • A developer pulled out a class to make code testable, but the extracted logic is too trivial to need its own tests.

Treatment

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

Payoff

What you get once it's gone.

  • There are fewer files, imports and constructor calls to follow when you read or debug a flow.
  • Related logic sits in one place, so the real responsibilities of the remaining class are easier to see.
  • There are fewer trivial tests and mocks to maintain.
  • New team members spend less time learning names that carry no meaning.

When to leave it

Sometimes it's not worth fixing.

  • The class is a value object that enforces an invariant or gives a primitive a distinct type, such as PickupCode rejecting strings that are not six digits. Small is fine when it prevents bugs.
  • The class is a deliberate seam for a dependency you really do substitute, such as a clock or a random source that tests replace to get deterministic results.
  • A framework or plugin system requires the class to exist, for example a handler or listener the container discovers by convention.
  • The extra behaviour is already being built in the current iteration, and the class will be filled out within days rather than someday.

Common questions

Short answers to the usual doubts.

How is a lazy class different from speculative generality?

Speculative generality is about reasons: the code exists for a future that never arrived. A lazy class is about size: the class does too little, whatever the reason. They often overlap, as HoldPeriod does in the example. A class can also become lazy simply because its work was moved elsewhere, with no speculation involved.

Doesn't inlining a class break the single responsibility principle?

Not if the inlined code serves the same purpose as its new home. Generating a pickup code and setting an expiry are both part of making a reservation. Single responsibility is about reasons to change, not about having as few lines as possible per class. If the inlined logic later grows its own rules and reasons to change, extract it again.

Isn't a tiny value object also a lazy class?

Not if it does real work. A wrapper that validates its input, prevents mixing up two strings or groups equality rules earns its place even with little code. The test is whether deleting it would lose a guarantee. If the wrapper only stores a value and returns it unchanged, it is lazy.

How should I raise this in a code review or interview?

Name the cost: one more file, one more name and one more indirection, with no behaviour or invariant in return. Then propose a concrete fix, usually inline-class into the single caller. Also say what would change your mind, for example a planned second implementation or a need to replace it in tests. That shows judgment, not just pattern matching.

Next guide5 of 6 · Smells: dead weightData Class: Fields and Getters With the Logic Living ElsewhereRead the next one