Skip to content

Dead Code: Unused Functions, Branches and Flags

How to spot code that never runs, like unused functions, unreachable branches and permanently-off flags, and how to delete it safely.

7 min readCode smellDead weight

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); // 12342

This 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.

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'.

Next guide3 of 6 · Smells: dead weightSpeculative Generality: Abstractions Built for Needs That Never CameRead the next one