Dead code is anything in your codebase that can no longer affect behaviour. That includes a function with no callers, a branch whose condition is never true, a parameter every caller leaves at its default, or a feature flag that was switched off two years ago and never removed. It rarely looks dead. It compiles, it has tests, and it sometimes has a comment saying it is kept just in case. Nobody deletes it because nobody is sure it is safe to. So every future reader has to work out whether it matters, every refactor has to carry it along, and every search turns it up as a false lead. In interviews this smell comes up in code review exercises and in questions about legacy systems. Interviewers want to see that you can prove code is unreachable and remove it with confidence, rather than leaving it in place out of caution.
Signs
How to spot it in a review.
- A function or class has zero call sites, or its only callers are its own tests.
- A boolean parameter or feature flag has the same value at every call site and in every environment.
- A branch checks for a value the rest of the system can no longer produce, such as a retired currency, region or status.
- Comments like 'kept just in case', 'old version' or 'do not delete' sit above code nobody can explain.
- Commented-out blocks of code that have been in place for months.
- Your IDE or linter greys out a symbol as unused, and the warning has been suppressed or ignored.
What it looks like
A typical case, in three languages.
type Country = "DE" | "FR" | "NL";
interface Booking {
memberId: string;
hours: number;
hourlyRateCents: number;
country: Country;
currency: string;
}
const VAT_RATES: Record<Country, number> = { DE: 0.19, FR: 0.2, NL: 0.21 };
// Old flat-rate tax engine. Replaced, but kept "in case finance asks".
function legacyTaxCents(netCents: number): number {
return Math.round(netCents * 0.16);
}
// Loyalty programme ended in March 2022. No callers left.
export function loyaltyDiscountCents(booking: Booking): number {
return booking.hours >= 40 ? 500 : 0;
}
export function invoiceTotalCents(booking: Booking, useLegacyTax = false): number {
const netCents = booking.hours * booking.hourlyRateCents;
let taxCents: number;
if (useLegacyTax) {
taxCents = legacyTaxCents(netCents);
} else {
taxCents = Math.round(netCents * VAT_RATES[booking.country]);
}
if (booking.currency === "GBP") {
// London desk closed in 2020; checkout only accepts EUR now.
return Math.round((netCents + taxCents) * 0.86);
}
return netCents + taxCents;
}
const total = invoiceTotalCents({
memberId: "m-118",
hours: 12,
hourlyRateCents: 850,
country: "NL",
currency: "EUR",
});
console.log(total); // 12342from dataclasses import dataclass
VAT_RATES = {"DE": 0.19, "FR": 0.2, "NL": 0.21}
@dataclass(frozen=True)
class Booking:
member_id: str
hours: int
hourly_rate_cents: int
country: str
currency: str
# Old flat-rate tax engine. Replaced, but kept "in case finance asks".
def _legacy_tax_cents(net_cents: int) -> int:
return int(round(net_cents * 0.16))
# Loyalty programme ended in March 2022. No callers left.
def loyalty_discount_cents(booking: Booking) -> int:
return 500 if booking.hours >= 40 else 0
def invoice_total_cents(booking: Booking, use_legacy_tax: bool = False) -> int:
net_cents = booking.hours * booking.hourly_rate_cents
if use_legacy_tax:
tax_cents = _legacy_tax_cents(net_cents)
else:
tax_cents = int(round(net_cents * VAT_RATES[booking.country]))
if booking.currency == "GBP":
# London desk closed in 2020; checkout only accepts EUR now.
return int(round((net_cents + tax_cents) * 0.86))
return net_cents + tax_cents
if __name__ == "__main__":
total = invoice_total_cents(
Booking(member_id="m-118", hours=12, hourly_rate_cents=850, country="NL", currency="EUR")
)
print(total) # 12342<?php
declare(strict_types=1);
final class Booking
{
public function __construct(
public readonly string $memberId,
public readonly int $hours,
public readonly int $hourlyRateCents,
public readonly string $country,
public readonly string $currency,
) {}
}
const VAT_RATES = ['DE' => 0.19, 'FR' => 0.2, 'NL' => 0.21];
// Old flat-rate tax engine. Replaced, but kept "in case finance asks".
function legacyTaxCents(int $netCents): int
{
return (int) round($netCents * 0.16);
}
// Loyalty programme ended in March 2022. No callers left.
function loyaltyDiscountCents(Booking $booking): int
{
return $booking->hours >= 40 ? 500 : 0;
}
function invoiceTotalCents(Booking $booking, bool $useLegacyTax = false): int
{
$netCents = $booking->hours * $booking->hourlyRateCents;
if ($useLegacyTax) {
$taxCents = legacyTaxCents($netCents);
} else {
$taxCents = (int) round($netCents * VAT_RATES[$booking->country]);
}
if ($booking->currency === 'GBP') {
// London desk closed in 2020; checkout only accepts EUR now.
return (int) round(($netCents + $taxCents) * 0.86);
}
return $netCents + $taxCents;
}
$total = invoiceTotalCents(new Booking('m-118', 12, 850, 'NL', 'EUR'));
echo $total . PHP_EOL; // 12342This is the invoicing module of Harbor Desk, a coworking booking platform. Three pieces are dead. loyaltyDiscountCents has no callers. No call site passes useLegacyTax, so it is always false, and legacyTaxCents only runs from that branch. The GBP branch cannot fire because checkout rejects every currency except EUR. Once all three are removed, invoiceTotalCents becomes three lines: net, tax, sum. Whoever adds the next country's VAT rate no longer has to work out whether the 0.16 rate or the 0.86 conversion factor still matters.
Why it happens
How code ends up like this.
- Every reader has to work out whether the code matters before they can ignore it. That cost repeats on every visit.
- Dead branches hide the real rules. Here a reader could easily think Harbor Desk still bills in GBP or still applies a flat 16% tax.
- Refactors, upgrades and type changes have to keep dead code compiling, which doubles the work for no benefit.
- Dead code can come back to life by accident, for example when someone sets a forgotten flag to true or reuses an old currency code, and then it runs logic nobody has checked in years.
- Tests that cover dead code inflate coverage numbers and make the suite slower without protecting any real behaviour.
Treatment
The refactorings that cure it, and when to choose each.
- Remove Flag Argument: Replace a Boolean with Explicit Functions
A boolean parameter like
useLegacyTaxhas the same value at every call site. Delete the parameter and the branch it controls, and keep only the path that actually runs. - Change Function Declaration: Fix a Signature Without Breaking Callers
A parameter is accepted but no longer read, or callers keep passing a value that has no effect. Remove it from the signature and update every caller in the same change.
- Inline Function: Remove Indirection That Adds Nothing
A helper is left with a single trivial caller after you remove a dead branch. Fold it back in so the leftover indirection does not suggest more variation than really exists.
- Consolidate Conditional: Merge Checks That Share a Result
Removing dead cases leaves several checks that now lead to the same outcome. Merge them so the conditional states the one rule that is still true.
- Inline Class: Fold a Class That No Longer Earns Its Keep
A class has been reduced to one or two live methods after its dead members were deleted. Move what is left into the class that uses it and remove the empty shell.
Payoff
What you get once it's gone.
- Shorter modules that describe only what the system really does today.
- Faster onboarding and code review, because nobody has to investigate code paths that never run.
- Cheaper refactors and dependency upgrades, because there is less code to keep compiling.
- No risk of a forgotten branch being switched back on by accident.
- Test suites and coverage reports that reflect live behaviour.
When to leave it
Sometimes it's not worth fixing.
- Public APIs of a library or SDK with external consumers. Your own repository cannot show you their call sites, so deprecate first and remove the code in a later major version.
- Kill switches and operational flags that are off on purpose and kept ready for incidents. Document them so nobody mistakes them for leftovers.
- Code that frameworks reach through reflection, configuration, routing tables or dependency injection. Before deleting it, check config files, string-based lookups and runtime logs, not just static references.
Common questions
Short answers to the usual doubts.
How can I prove code is dead before I delete it?
Combine several kinds of evidence. Use your IDE or a static analyser to find references, and also search the repository for the name as a string to catch reflection and config. For branches, check what values the inputs can actually take, for example what checkout accepts. If you are still unsure, add a log line or metric to the suspect path, deploy it, and watch for a release cycle. A path with zero hits over a representative period is a safe candidate for deletion.
Shouldn't I keep old code in case we need it again?
No. Version control already keeps it. Delete the code and write a clear commit message, for example 'remove flat-rate legacy tax engine, replaced by per-country VAT', so anyone can find and restore it later. If it is ever needed again, the requirements will have changed anyway and the old code will need rework.
What about commented-out code?
Treat it as dead code and delete it. It cannot be compiled, tested or refactored, so it goes stale quickly, and readers cannot tell whether it is a reference, an experiment or a pending fix. If it records a decision, replace it with a short comment explaining the decision.
How do I clean up feature flags that have been rolled out to 100%?
Treat the flag's current value as a constant. Remove the flag check, delete the branch that can no longer run, then remove the flag from your flag service and configuration. Do all of this in one small change. It helps to give each new flag an owner and a removal date when you create it, so the cleanup is planned from the start.
How do I talk about dead code in a code review interview?
Point to the specific line and explain why it cannot run: no callers, a constant flag, or an impossible input. Then say how you would confirm it, for example by searching for references, checking config, or adding a metric, and how you would remove it safely in a small, separate commit. Interviewers want to see both that you notice it and that you are disciplined about verifying it, not just a quick 'delete this'.