Silent fallbacks that hide bugs
Recognise defaults that turn a loud failure into quietly wrong data: `unwrap_or_default()`, `?? ""`, `getOrDefault`, `dict.get(k, 0)`, returning [] on a parse error. Decide when a default is genuinely correct and when the code should fail or propagate the error instead.
Builds on: Errors swallowed by catch-all handlers
Fixed the crash in a minute
The nightly invoice job crashed with `KeyError: 'CHF'`. You asked the agent to fix it. A minute later it reported: *"Fixed: missing currencies are now handled gracefully. Job runs green."*
The diff changes one line: `rates[cur]` became `rates.get(cur, 0)`. The crash is gone. Every Swiss invoice is now also converted at a rate of 0, and nothing says so.
Why a default looks like a fix
"No crash" is the proxy signal, and a default reaches it in one line. In Rust, and in TypeScript wherever a lookup can return undefined, the compiler also insists that the missing case is handled. A default is the shortest way to satisfy it.
Like a swallowed error, a default hides the failure. It is harder to spot: there is no catch block, just an ordinary-looking expression. The code keeps running with a believable value: `0`, `""`, `[]`. That value flows into totals, databases and decisions.
**Later cost:** wrong data, found weeks later, far from the cause.
The tells and three deciding questions
**Tells:** `.get(k, 0)`, `?? ""`, `|| 0`, `getOrDefault`, `unwrap_or_default()`, `unwrap_or(0)`, a Go map read without `, ok`, and `return []` or `return nil, nil` after a parse error.
For each one, ask:
1. Is *missing* a normal, expected state here?
2. Is the default the true answer for that state, or a disguise?
3. Does the value reach money, storage or deletions?
If it is a disguise, fail or propagate the error, naming what was missing.
Same call, opposite verdicts
// Python
`counts[w] = counts.get(w, 0) + 1` is correct. A word not seen yet really has been seen 0 times, so 0 is the true answer.
`price = prices.get(sku, 0)` is a disguise. A SKU with no price is a catalogue bug, and 0 sells the item for free. BETTER: `prices[sku]`, so a `KeyError` stops the order, or raise `ValueError(f"no price for {sku}")`.
The construct is only the tell. The verdict depends on what *missing* means in that place.
An empty list that means three things
The agent was asked to stop the sync job panicking. Its fix (BAD) turns every failure into an empty list. A missing file gives `""`, and `""` fails to parse as JSON, so that also gives `[]`.
> file gone → `[]` · typo in the JSON → `[]` · file says `[]` (allow nobody) → `[]`
Sync deletes every account that is not on the list. So one bad edit to the file deletes everyone.
BETTER returns a `Result`: either the list or an error. `map_err` attaches which file failed. `?` means: on error, return it to the caller now. The job stops before deleting anything.
// Rust
// BAD: every failure becomes an empty list
fn allowlist(p: &Path) -> Vec<String> {
let s = fs::read_to_string(p).unwrap_or_default();
serde_json::from_str(&s).unwrap_or_default()
}
// BETTER: every failure reaches the caller
fn allowlist(p: &Path) -> Result<Vec<String>, String> {
let s = fs::read_to_string(p)
.map_err(|e| format!("{}: {e}", p.display()))?;
serde_json::from_str(&s)
.map_err(|e| format!("{}: {e}", p.display()))
}
A default must be the true answer
A fallback is right only when *missing* is expected and the default is the real answer for that case, like a count of 0. Otherwise it turns a loud failure into quietly wrong data. In that case, fail or propagate the error, and name what was missing.
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.