Skip to content
Architecture & TypesChapter 22 / 57Advanced6 min read

Fix Root Causes, Remove Don't Gate

Root cause analysis and the right use of feature flags. Fix a wrong null at its producer, and delete unwanted code instead of hiding it behind a flag.

On this page

1. What is this practice?

This practice has two parts:

  • Fix root causes: if undefined shows up on the frontend, the problem is often not in the frontend. It is in the backend query, mapping or contract. Trace it back there.
  • Remove, don't gate: the old checkout, the old pricing rule, a half built feature. Instead of leaving them behind an if (flag), take them out. A flag is only for behavior that truly changes at runtime.

2. Why does it matter?

A real story: on the profile page some users' names showed as "Unknown" because someone had written user.name ?? "Unknown". Months later it turned out that a signup query was not saving the name at all. Thousands of users had incomplete data, and emails were going out saying "Hi Unknown". The fallback had hidden the bug.

A flag story: the legacyPricing flag had been false for a year. Someone copied a config and set it to true by mistake. The old code that nobody tested ran in production and wrong prices were charged.

Hiding the symptom and gatingFix the root cause, delete the dead path
A fallback makes the screen look fine, the data stays brokenThe producer is fixed, the data is right, the screen is right
Every new consumer has to write the same guardOne fix, every consumer benefits
The bug stays hidden for monthsThe bug shows up the first time
Two code paths, both need testingOne path, one test
A switched off flag can be switched on by mistake any timeThe code is gone, so there is nothing to switch on

3. When to follow it

LevelHow importantWhy
Learning projectWhen an error appears, build the habit of finding the cause, not hiding it with ?.This is the core skill of debugging
Production codeA must. Defensive fallbacks on internal data and useless flags get stopped in reviewHidden bugs corrupt data that is expensive to repair later
Senior levelA team culture of tracing to the producer, and flags that expireA senior is the one who carries the problem to another team's code

4. Bad vs good code

Bad: a guard on the consumer

tsx
export function AuthorName({ post }: AuthorNameProps) {
  return <span>{post.author?.name ?? "Unknown"}</span>;
}

The problem was that the service query was not including the author at all:

ts
export function list() {
  return db.post.findMany({ select: { id: true, title: true } });
}

Good: fix the producer, make the contract strict

ts
export function list() {
  return db.post.findMany({
    select: { id: true, title: true, author: { select: { name: true } } },
  });
}
ts
export type PostWithAuthor = Awaited<ReturnType<typeof list>>[number];
tsx
export function AuthorName({ post }: AuthorNameProps) {
  return <span>{post.author.name}</span>;
}

Now the type itself says author.name is always there. If the author is ever removed from the query, the build breaks instead of "Unknown" showing on screen.

Bad: the old path behind a flag

ts
export function total(cart: Cart) {
  if (flags.legacyPricing) {
    return legacyTotal(cart);
  }

  return currentTotal(cart);
}

Good: delete the old path

ts
export function total(cart: Cart) {
  return currentTotal(cart);
}

legacyTotal, the flag and its tests are all deleted. If you ever need the old code, it is in git history.

What is the difference?

ThingBadGood
Missing author"Unknown" in the UIIncluded in the query, type is non-null
When the bug showsMonths laterAt build or in the first test
Old pricingAlive behind a flagDeleted
Test surfaceTwo pathsOne path

5. How to apply it

Getting to the root cause
Reproduce the bug
Trace the wrong value backwards: component, hook, client, route, service, query
Fix it where the value first became wrong
Write a test there that catches this bug
Remove the useless guards downstream

Before adding a flag, ask: does this behavior truly change at runtime? Like a gradual rollout, an A/B test or a kill switch. If yes, the flag is fine, but also write down its expiry date. If the answer is "we might need it later", delete it.

6. Exceptions and common mistakes

Outside data that you do not control (a third-party API, user input) must be validated at the boundary. That is not a defensive fallback, it is necessary. The rule is about internal contracts, where the producer is your own code.

When production is on fire, adding a temporary guard is fine, but also create a task for the root fix, and remove the guard together with that fix. Feature flags are a basic tool for many teams (rollouts, experiments) and this rule is not against them. It is only against flags that exist out of "fear of deleting".

Mistake: Adding ?? [] or ?? {} on required data

Why? The contract says the value will be there. If it did not arrive, there is a bug in the producer, and the fallback makes that bug disappear.

Better: If the value is missing, throw a clear error and trace it back to the producer.

Mistake: The backend belongs to another team, so add a guard on the frontend

Why? Every consumer writes its own guard and the real problem never gets fixed.

Better: Find the producer in the backend, open an issue or a PR, and mark the guard as temporary until then.

Mistake: Keeping the old feature behind a flag, just in case

Why? Switched off code is not tested, it rots, and it can be switched on by mistake and break production.

Better: Delete it. Version control always keeps the old code.

7. What to learn before and after

Suggested order
Fail Fast, No Defensive Fallbacks
Fix Root Causes, Remove Don't Gate
Generated Database Types
Logging

Final quick revision

CheckYes / No
Traced the wrong value back to the producerYes
A fallback like ?? "Unknown" on internal dataNo
A test at the producer along with the fixYes
Unwanted code behind a flagNo, delete it
Wrote an expiry for every needed flagYes