Skip to content

Inline Class: Fold a Class That No Longer Earns Its Keep

Learn how to use Inline Class to merge a thin, pass-through class into the class that uses it, in small steps with tests passing after each one.

6 min readRefactoring

Inline Class is the reverse of Extract Class. You take a class that has shrunk to a field or two and a method or two, move everything it does into the one class that uses it, and delete it. In this guide you will apply it to a coffee subscription service for a small roastery called Ridgeline. You will see the code before and after in TypeScript, Python and PHP, the steps that keep your tests green, and when you should leave the class alone. In interviews it is a good way to show that you can tell a useful abstraction from a leftover one.

The problem

A situation where the obvious design starts to hurt.

When Ridgeline launched subscriptions, someone extracted a DeliverySchedule class on the assumption that schedules would get complicated: skip weeks, holiday pauses, regional cut-offs. None of that was built. Two years later DeliverySchedule holds one integer and has one real calculation, and Subscription is its only user.

Every question about delivery dates now goes through Subscription, which forwards the call to DeliverySchedule. To read the logic you have to open two files. To change it you have to touch both: the delegating method and the real one. The extra class adds indirection and gives you nothing for it.

Before and after

The same code, before and after the change.

Before

class DeliverySchedule {
  constructor(private readonly intervalDays: number) {}

  getIntervalDays(): number {
    return this.intervalDays;
  }

  nextDelivery(lastShipped: Date): Date {
    const next = new Date(lastShipped.getTime());
    next.setUTCDate(next.getUTCDate() + this.intervalDays);
    return next;
  }
}

class Subscription {
  private readonly schedule: DeliverySchedule;

  constructor(
    public readonly customer: string,
    public readonly blend: string,
    intervalDays: number,
  ) {
    this.schedule = new DeliverySchedule(intervalDays);
  }

  getIntervalDays(): number {
    return this.schedule.getIntervalDays();
  }

  nextDelivery(lastShipped: Date): Date {
    return this.schedule.nextDelivery(lastShipped);
  }
}

const sub = new Subscription("Priya Nair", "Ridgeline Espresso", 14);
const next = sub.nextDelivery(new Date(Date.UTC(2024, 2, 1)));
console.log(`${sub.customer}: every ${sub.getIntervalDays()} days, next ${next.toISOString().slice(0, 10)}`);
// Priya Nair: every 14 days, next 2024-03-15

After

class Subscription {
  constructor(
    public readonly customer: string,
    public readonly blend: string,
    private readonly intervalDays: number,
  ) {}

  getIntervalDays(): number {
    return this.intervalDays;
  }

  nextDelivery(lastShipped: Date): Date {
    const next = new Date(lastShipped.getTime());
    next.setUTCDate(next.getUTCDate() + this.intervalDays);
    return next;
  }
}

const sub = new Subscription("Priya Nair", "Ridgeline Espresso", 14);
const next = sub.nextDelivery(new Date(Date.UTC(2024, 2, 1)));
console.log(`${sub.customer}: every ${sub.getIntervalDays()} days, next ${next.toISOString().slice(0, 10)}`);
// Priya Nair: every 14 days, next 2024-03-15

Why it's worth it

What the change buys you.

A class should give you something in return for the extra file, name and indirection it costs: a hidden decision, a reusable concept, or a boundary another team can work behind. DeliverySchedule gives none of these. It has one client, its whole public surface is mirrored by Subscription, and the flexibility it was built for never arrived.

After the refactoring, the delivery rule lives next to the data it depends on. Someone reading Subscription sees the whole behaviour in one place, and a change to the interval logic touches one method instead of two. Behaviour is unchanged: the constructor takes the same arguments, both public methods return the same values, and the date arithmetic is identical.

Inlining is also often a first step, not the last. Once the leftover pieces are merged into one class, you can see the real responsibilities and, if needed, extract a better-shaped class later.

  • One class to read and change instead of two, with no forwarding methods in between.
  • Delivery logic sits next to the data it uses, so the behaviour of a subscription is visible in one file.
  • Fewer types, constructors and test fixtures to maintain.
  • Frees you to re-extract a better-shaped class later if real complexity shows up.

Careful with

  • Do not inline a class that has several clients. Each of them would have to depend on the absorbing class, which usually makes coupling worse.
  • A small class is not automatically a lazy one. A value object that enforces rules, such as a positive interval or a currency, earns its place even with little code.
  • If the class sits behind an interface used for testing or as a plugin point, check whether the seam is still needed before removing it.
  • If the absorbing class is already large, inlining more into it can create a large class. Consider a different home or a fresh extraction instead.

Step by step

Small, safe steps, with the tests passing after each.

  1. Confirm DeliverySchedule has no clients other than Subscription by searching the codebase, including tests and dependency injection configuration. Run the tests to get a green baseline.

  2. Add a private intervalDays field to Subscription and set it in the constructor alongside the existing schedule field. Run the tests.

  3. Change Subscription.getIntervalDays to return its own field instead of delegating to schedule. Run the tests.

  4. Copy the body of DeliverySchedule.nextDelivery into Subscription.nextDelivery, using the new field, and remove the delegation. Run the tests.

  5. Remove the schedule field and the line that constructs it. Run the tests.

  6. Move any tests written directly against DeliverySchedule so they exercise Subscription instead, then delete DeliverySchedule. Run the full suite and commit.

Smells it removes

Smells this refactoring helps remove.

Common questions

Short answers to the usual doubts.

How do I tell a lazy class from a small but useful one?

Ask what the class protects or names. If it enforces an invariant, hides a decision likely to change, or is used from several places, keep it. If it only stores a value and forwards calls for a single client, and you cannot name a concrete change it would absorb, it is a candidate for inlining.

What is the difference between Inline Class and Remove Middle Man?

Remove Middle Man keeps both classes and lets clients talk directly to the delegate. Inline Class deletes one class and moves its features into the other. Use Inline Class when the delegate itself has too little to justify existing.

Is it safe to inline a class in a single commit?

For a class as small as this one, the whole change fits in one commit, but do it as a sequence of steps with the tests run after each. That way if a test fails you know exactly which move caused it.

How would I explain this refactoring in an interview?

Describe the trade-off: every class costs indirection, and this one returned nothing for it. Then walk through the safe steps, mention that you checked for other clients first, and note when you would not do it, such as for value objects or shared types.

Next guide4 of 5 · Techniques: moving featuresHide Delegate: Stop Callers Reaching Through an Object's InternalsRead the next one