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-15from datetime import date, timedelta
class DeliverySchedule:
def __init__(self, interval_days: int) -> None:
self._interval_days = interval_days
def interval_days(self) -> int:
return self._interval_days
def next_delivery(self, last_shipped: date) -> date:
return last_shipped + timedelta(days=self._interval_days)
class Subscription:
def __init__(self, customer: str, blend: str, interval_days: int) -> None:
self.customer = customer
self.blend = blend
self._schedule = DeliverySchedule(interval_days)
def interval_days(self) -> int:
return self._schedule.interval_days()
def next_delivery(self, last_shipped: date) -> date:
return self._schedule.next_delivery(last_shipped)
sub = Subscription("Priya Nair", "Ridgeline Espresso", 14)
next_date = sub.next_delivery(date(2024, 3, 1))
print(f"{sub.customer}: every {sub.interval_days()} days, next {next_date.isoformat()}")
# Priya Nair: every 14 days, next 2024-03-15<?php
declare(strict_types=1);
final class DeliverySchedule
{
public function __construct(private readonly int $intervalDays)
{
}
public function getIntervalDays(): int
{
return $this->intervalDays;
}
public function nextDelivery(DateTimeImmutable $lastShipped): DateTimeImmutable
{
return $lastShipped->modify("+{$this->intervalDays} days");
}
}
final class Subscription
{
private readonly DeliverySchedule $schedule;
public function __construct(
public readonly string $customer,
public readonly string $blend,
int $intervalDays,
) {
$this->schedule = new DeliverySchedule($intervalDays);
}
public function getIntervalDays(): int
{
return $this->schedule->getIntervalDays();
}
public function nextDelivery(DateTimeImmutable $lastShipped): DateTimeImmutable
{
return $this->schedule->nextDelivery($lastShipped);
}
}
$sub = new Subscription('Priya Nair', 'Ridgeline Espresso', 14);
$next = $sub->nextDelivery(new DateTimeImmutable('2024-03-01', new DateTimeZone('UTC')));
echo "{$sub->customer}: every {$sub->getIntervalDays()} days, next {$next->format('Y-m-d')}\n";
// Priya Nair: every 14 days, next 2024-03-15After
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-15from datetime import date, timedelta
class Subscription:
def __init__(self, customer: str, blend: str, interval_days: int) -> None:
self.customer = customer
self.blend = blend
self._interval_days = interval_days
def interval_days(self) -> int:
return self._interval_days
def next_delivery(self, last_shipped: date) -> date:
return last_shipped + timedelta(days=self._interval_days)
sub = Subscription("Priya Nair", "Ridgeline Espresso", 14)
next_date = sub.next_delivery(date(2024, 3, 1))
print(f"{sub.customer}: every {sub.interval_days()} days, next {next_date.isoformat()}")
# Priya Nair: every 14 days, next 2024-03-15<?php
declare(strict_types=1);
final class Subscription
{
public function __construct(
public readonly string $customer,
public readonly string $blend,
private readonly int $intervalDays,
) {
}
public function getIntervalDays(): int
{
return $this->intervalDays;
}
public function nextDelivery(DateTimeImmutable $lastShipped): DateTimeImmutable
{
return $lastShipped->modify("+{$this->intervalDays} days");
}
}
$sub = new Subscription('Priya Nair', 'Ridgeline Espresso', 14);
$next = $sub->nextDelivery(new DateTimeImmutable('2024-03-01', new DateTimeZone('UTC')));
echo "{$sub->customer}: every {$sub->getIntervalDays()} days, next {$next->format('Y-m-d')}\n";
// Priya Nair: every 14 days, next 2024-03-15Why 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.
Confirm
DeliverySchedulehas no clients other thanSubscriptionby searching the codebase, including tests and dependency injection configuration. Run the tests to get a green baseline.Add a private
intervalDaysfield toSubscriptionand set it in the constructor alongside the existingschedulefield. Run the tests.Change
Subscription.getIntervalDaysto return its own field instead of delegating toschedule. Run the tests.Copy the body of
DeliverySchedule.nextDeliveryintoSubscription.nextDelivery, using the new field, and remove the delegation. Run the tests.Remove the
schedulefield and the line that constructs it. Run the tests.Move any tests written directly against
DeliveryScheduleso they exerciseSubscriptioninstead, then deleteDeliverySchedule. 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.