Skip to content

Shotgun Surgery: When One Change Means Edits in Many Files

Shotgun Surgery is when a single logical change forces small edits across many files, and how to pull that knowledge back into one place.

7 min readCode smellRigidity

You get a ticket that sounds small: add a new membership tier, support a new currency, rename a status. Then you open the pull request and it touches nine files, each with a two-line tweak. That is Shotgun Surgery. One decision has been spread thin across the codebase, so every change to it is a scattershot of edits. The danger is not the typing. It is the edit you forget. When the knowledge lives in many places, nothing tells you that you have found them all, and the one you miss usually fails quietly in production. In interviews this smell comes up when you are asked how you would make a design easier to change. Being able to name it, spot it in a diff and explain how to consolidate it shows you think about the cost of change, not just correctness today.

Signs

How to spot it in a review.

  • A routine feature request, such as adding an enum value, produces a diff that touches many files with tiny edits in each.
  • The same string or code, like "dedicated" or "EUR", is compared against in switch statements or if chains across several modules.
  • Git history shows the same group of files changing together in commit after commit.
  • Bugs appear where one place was updated and a sibling place was not, often hidden by a default branch.
  • Code reviewers regularly ask "did you also update X?" and the answer depends on someone remembering.

What it looks like

A typical case, in three languages.

// BEFORE: tier knowledge scattered across four modules

// billing/pricing.ts
export function monthlyPriceCents(tier: string): number {
  switch (tier) {
    case "flex": return 9_900;
    case "dedicated": return 34_900;
    case "office": return 89_900;
    default: throw new Error(`Unknown tier: ${tier}`);
  }
}

// booking/limits.ts
export function maxBookingsPerWeek(tier: string): number {
  if (tier === "flex") return 3;
  if (tier === "dedicated") return 7;
  return Infinity; // office
}

// access/doors.ts
export function canEnterAt(tier: string, hour: number): boolean {
  if (tier === "flex") return hour >= 8 && hour < 19;
  return true;
}

// invoices/labels.ts
export function invoiceLabel(tier: string): string {
  if (tier === "office") return "Private Office";
  if (tier === "dedicated") return "Dedicated Desk";
  return "Flex Desk";
}

// AFTER: one module owns everything about a tier

// membership/tier.ts
export class MembershipTier {
  constructor(
    readonly code: string,
    readonly label: string,
    readonly monthlyPriceCents: number,
    readonly maxBookingsPerWeek: number,
    readonly openHour: number,
    readonly closeHour: number,
  ) {}

  canEnterAt(hour: number): boolean {
    return hour >= this.openHour && hour < this.closeHour;
  }

  static fromCode(code: string): MembershipTier {
    const tier = TIERS.get(code);
    if (!tier) throw new Error(`Unknown tier: ${code}`);
    return tier;
  }
}

const TIERS = new Map<string, MembershipTier>([
  ["flex", new MembershipTier("flex", "Flex Desk", 9_900, 3, 8, 19)],
  ["dedicated", new MembershipTier("dedicated", "Dedicated Desk", 34_900, 7, 0, 24)],
  ["office", new MembershipTier("office", "Private Office", 89_900, Infinity, 0, 24)],
]);

// Callers elsewhere:
// const tier = MembershipTier.fromCode(member.tierCode);
// invoice.addLine(tier.label, tier.monthlyPriceCents);
// if (!tier.canEnterAt(now.getHours())) door.deny();

DeskHop, a coworking booking platform, wants to launch a fourth tier, Studio. In the before version that means four edits in four modules owned by three teams. Worse, three of those functions end in a default branch, so if you forget booking limits, Studio members silently get unlimited bookings, and if you forget door access they get 24-hour entry. After moving every tier fact into MembershipTier, adding Studio is one new line, and an unknown code fails loudly in one place instead of falling through quietly.

Why it happens

How code ends up like this.

  • Every forgotten spot is a latent bug, and default branches or fallback values tend to hide the omission until a customer hits it.
  • Small features turn into cross-team pull requests, which slows delivery and makes reviews harder because the real change is diluted across many files.
  • The rule has no single home, so nobody can read one place and know how a tier, currency or status actually behaves.
  • Onboarding suffers: new engineers only learn the full list of places to touch by breaking something or asking someone who remembers.

Treatment

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

Payoff

What you get once it's gone.

  • A change to the concept becomes a change to one file, which is easier to write, review and revert.
  • Omissions turn into compile errors or loud runtime failures instead of silent wrong behaviour.
  • The rule is readable in one place, so product questions like what a Flex member gets can be answered by reading a single class.
  • Tests can target the consolidated class directly rather than re-checking each scattered function.

When to leave it

Sometimes it's not worth fixing.

  • The edits are spread because the layers really are independent, such as a database migration, an API schema and a UI form for a new field. Consolidating them would couple layers that should stay separate.
  • The concept is genuinely stable, for example the days of the week, and the cost of gathering it exceeds the cost of the rare change.
  • Generated code or framework conventions require the repetition, and the generator or convention is the real single source of truth.

Common questions

Short answers to the usual doubts.

How is Shotgun Surgery different from Divergent Change?

They are mirror images. Divergent Change is one module that changes for many unrelated reasons, so it needs splitting. Shotgun Surgery is one reason that forces changes in many modules, so the pieces need gathering. The fix for one can create the other if you split or merge too aggressively, so aim for each reason to change mapping to one place.

How do I find Shotgun Surgery in an existing codebase?

Look at version control. Files that are repeatedly modified in the same commits are strong candidates, and many teams run simple change-coupling queries over git log for this. You can also search for every comparison against a specific string or enum value: if it shows up in many modules, the knowledge is scattered.

Is a config file or lookup table enough to fix it?

Often it is a good first step, because it puts the values in one place. But if behaviour still branches on the code in several modules, the smell remains. The goal is that callers ask the concept for an answer rather than deciding for themselves based on its code.

Will consolidating everything into one class create a God Class?

It can if you pull in unrelated responsibilities. Gather only what changes together for the same reason. A tier class that knows price, limits and access hours is cohesive; one that also sends emails and renders invoices is not.

How should I talk about this smell in an interview?

Describe it in terms of change cost: a single requirement change forces edits in many places, and the risk is the place you miss. Then give a concrete fix such as moving the scattered decisions onto one type, and mention the trade-off that you should not merge things that change for different reasons.

Next guide3 of 3 · Smells: rigidityParallel Inheritance HierarchiesRead the next one