Skip to content

Speculative Generality: Abstractions Built for Needs That Never Came

How to spot interfaces, hooks, parameters and factories written for imagined future requirements, and how to strip them back to what the code actually needs.

7 min readCode smellDead weight

Speculative generality is code that exists because someone thought it might be needed later. Examples include an interface with one implementation, a factory that can only build one thing, a parameter every caller leaves at its default, and lifecycle hooks nobody overrides. Each one looked like cheap insurance when it was written. Months later it is just more code to read. The cost is real even though the code does nothing. Every reader has to work out whether the extension point matters, every change has to pass through the extra layers, and the abstraction often guesses the future wrong, so it gets in the way when the real second case finally shows up. In an interview, spotting this smell shows you can tell flexibility the code needs apart from flexibility someone only imagined.

Signs

How to spot it in a review.

  • An interface or abstract base class with exactly one concrete implementation, and no plan or ticket for a second one.
  • Hook methods such as beforeSend or onInit that are empty in the base class and never overridden anywhere.
  • Function parameters or constructor arguments that every call site omits or passes with the same value.
  • A factory or registry whose only branch builds a single class, plus a throw for every other input.
  • Names like Base, Generic, Pluggable or Provider wrapped around code that has only ever had one variant.
  • Tests are the only callers of a method, a flag or an option.

What it looks like

A typical case, in three languages.

interface SmsGateway {
  post(message: { to: string; text: string }): Promise<void>;
}

interface Appointment {
  patientPhone: string;
  patientName: string;
  startsAt: Date;
  clinicName: string;
}

interface ReminderChannel {
  send(to: string, body: string): Promise<void>;
}

abstract class BaseReminderChannel implements ReminderChannel {
  protected beforeSend(_to: string, _body: string): void {}
  protected afterSend(_to: string): void {}

  async send(to: string, body: string): Promise<void> {
    this.beforeSend(to, body);
    await this.deliver(to, body);
    this.afterSend(to);
  }

  protected abstract deliver(to: string, body: string): Promise<void>;
}

class SmsChannel extends BaseReminderChannel {
  constructor(private readonly gateway: SmsGateway) {
    super();
  }

  protected async deliver(to: string, body: string): Promise<void> {
    await this.gateway.post({ to, text: body });
  }
}

class ReminderChannelFactory {
  static create(kind: string, gateway: SmsGateway): ReminderChannel {
    if (kind === "sms") {
      return new SmsChannel(gateway);
    }
    throw new Error(`Unsupported reminder channel: ${kind}`);
  }
}

const DEFAULT_TEMPLATE =
  "Hi {name}, reminder: your appointment at {clinic} is at {time}.";

class AppointmentReminder {
  constructor(
    private readonly channel: ReminderChannel,
    private readonly locale: string = "en-GB",
    private readonly templateResolver?: (locale: string) => string,
  ) {}

  async remind(appt: Appointment): Promise<void> {
    const template = this.templateResolver?.(this.locale) ?? DEFAULT_TEMPLATE;
    const time = appt.startsAt.toTimeString().slice(0, 5);
    const body = template
      .replace("{name}", appt.patientName)
      .replace("{clinic}", appt.clinicName)
      .replace("{time}", time);
    await this.channel.send(appt.patientPhone, body);
  }
}

export { AppointmentReminder, ReminderChannelFactory };
export type { Appointment, SmsGateway };

This is the reminder module of a physiotherapy booking product. It has sent SMS reminders and nothing else for three years. Nobody overrides beforeSend or afterSend, the factory is always called with "sms", every caller relies on the default en-GB locale, and no caller has ever passed a template resolver. Delete the base class, the factory and the two unused constructor arguments, and what remains is an SmsReminder class with one method that does exactly what the system does today.

Why it happens

How code ends up like this.

  • A developer adds extension points to avoid a feared future rewrite, but the feature they were planning for gets cut or never gets prioritised.
  • Design patterns get applied by default, so a factory or strategy interface appears before a second variant exists.
  • A framework or library mindset gets carried into application code, where the only consumers are in the same repository and are easy to change.
  • Features are removed but their scaffolding is left behind: the second implementation goes, while the interface, the flag and the hook stay.
  • Code review rewards code that looks extensible, so a simpler design feels like cutting corners.

Treatment

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

Payoff

What you get once it's gone.

  • Fewer files and types to read before you understand what the code actually does.
  • Changes touch one class instead of threading through an interface, a base class and a factory.
  • When a real second variant does appear, you can design the abstraction around two known cases instead of one guess.
  • Tests get simpler because they no longer have to cover hooks and options that production never uses.

When to leave it

Sometimes it's not worth fixing.

  • You are writing a published library or SDK, where external consumers depend on the extension points and you cannot see their code.
  • The second implementation is already scheduled and in progress, not just a possibility someone mentioned.
  • An interface exists to provide a test seam around a slow or external dependency, and the tests really do rely on it.
  • A plugin boundary is required by a framework or deployment model, for example a hook that an external system calls into.

Common questions

Short answers to the usual doubts.

Isn't an interface with one implementation fine if it helps testing?

It can be. If tests swap in a fake through that interface, it has a second implementation and earns its place. The smell applies when nothing, including the tests, ever uses another implementation. Many languages can also mock concrete classes or take a function as a dependency, so check whether you need the interface at all.

How is speculative generality different from YAGNI?

YAGNI is the principle: don't build something until you need it. Speculative generality is the symptom you find in code where that principle was ignored. When you name the smell in a review, you point at specific artefacts such as unused hooks, a one-branch factory or dead parameters, and you can propose a concrete refactoring for each.

What if the feature really does arrive next year?

Then you add the abstraction next year, when you know what the second case looks like. Extracting an interface from two working classes is a routine refactoring. Fixing an abstraction that guessed wrong is usually harder, because the existing code has been shaped around the wrong assumptions.

How do I prove a hook or parameter is unused before deleting it?

Search the whole codebase for overrides and call sites, including tests, configuration and reflection-based lookups such as string class names. In a typed language, delete the hook or parameter and let the compiler report the breakages. Check usage logs or feature flags for anything exposed at runtime. If the code is only reachable from tests, the tests are the only thing to update.

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

Name the specific dead extension point, explain what it costs readers and maintainers, and propose the smaller design. Then say what would make you add the abstraction back, for example a second channel on the roadmap. That shows you are judging the trade-off rather than following a rule.

Next guide4 of 6 · Smells: dead weightLazy Class: When a Class Doesn't Earn Its PlaceRead the next one