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),
};
}
}import secrets
from dataclasses import dataclass
from datetime import datetime, timedelta, timezone
class PickupCodeGenerator:
def __init__(self, length: int = 6) -> None:
self.length = length
def generate(self) -> str:
return str(secrets.randbelow(10 ** self.length)).zfill(self.length)
class HoldPeriod:
def __init__(self, hours: int = 72) -> None:
self.hours = hours
def expires_from(self, now: datetime) -> datetime:
return now + timedelta(hours=self.hours)
@dataclass(frozen=True)
class Reservation:
locker_id: str
parcel_id: str
pickup_code: str
expires_at: datetime
class LockerReservationService:
def __init__(self, hold_hours: int = 72) -> None:
self._codes = PickupCodeGenerator()
self._hold = HoldPeriod(hold_hours)
def reserve(self, locker_id: str, parcel_id: str, now: datetime | None = None) -> Reservation:
now = now or datetime.now(timezone.utc)
return Reservation(
locker_id=locker_id,
parcel_id=parcel_id,
pickup_code=self._codes.generate(),
expires_at=self._hold.expires_from(now),
)<?php
final class PickupCodeGenerator
{
public function __construct(private readonly int $length = 6) {}
public function generate(): string
{
return str_pad((string) random_int(0, 10 ** $this->length - 1), $this->length, '0', STR_PAD_LEFT);
}
}
final class HoldPeriod
{
public function __construct(private readonly int $hours = 72) {}
public function expiresFrom(DateTimeImmutable $now): DateTimeImmutable
{
return $now->modify("+{$this->hours} hours");
}
}
final class Reservation
{
public function __construct(
public readonly string $lockerId,
public readonly string $parcelId,
public readonly string $pickupCode,
public readonly DateTimeImmutable $expiresAt,
) {}
}
final class LockerReservationService
{
private PickupCodeGenerator $codes;
private HoldPeriod $hold;
public function __construct(int $holdHours = 72)
{
$this->codes = new PickupCodeGenerator();
$this->hold = new HoldPeriod($holdHours);
}
public function reserve(string $lockerId, string $parcelId, ?DateTimeImmutable $now = null): Reservation
{
$now ??= new DateTimeImmutable('now', new DateTimeZone('UTC'));
return new Reservation(
$lockerId,
$parcelId,
$this->codes->generate(),
$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.
- Inline Class: Fold a Class That No Longer Earns Its Keep
The class has one caller, or a few, and its fields and methods fit naturally into the class that uses it. Move them across and delete the empty class.
- Inline Function: Remove Indirection That Adds Nothing
After inlining the class, a private method is now just an indirection around one standard library call and reads more clearly at the call site.
- Move Function: Put Behaviour Next to the Data It Uses
The lazy class has several callers. Its one method belongs on an existing class that already owns the related data, so move the method there before deleting the lazy class.
- Remove Middle Man: Let Callers Talk to the Real Object
The class only forwards calls to another object. Let callers talk to that object directly, then delete the forwarding class.
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
PickupCoderejecting 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.