general code and the edge-case tail · lesson 3 of 9 · sources checked 2026-11-30
the override layer
This lesson gives each special case its place in data, in an adapter, or in general code, with one precedence order and a generic fallback.
status of this lesson: from a public sourcepurpose
A special case in the wrong place makes the next change hard. A branch on a site name in general code grows until a fix for one site breaks a different site. A fixed place for each kind of special case keeps the general code clean and each special case easy to find and remove.
objectives
- Select the place of a special case: a data row, a behavior flag, an adapter, or a general rule.
- Write an override with a scope, an owner, an evidence reference, an expiry, and a hit counter.
- Compute the effective value of a behavior with a precedence order.
- Explain why a generic fallback returns an explicit unsupported result and not a weak success.
new terms
- behavior flag
- A named behavior in a table that a target turns on, so that general code asks about the behavior and not about the target.
- override precedence
- The order of scopes that gives one effective value when more than one override matches.
- generic fallback
- The last adapter, which tries general methods and returns an explicit unsupported result when it finds nothing.
how it works
A special case has four possible places. The rule is simple: put each special case in the cheapest place that can hold it.
| place | what goes there | example |
|---|---|---|
| Data row | A changed value | A date format for one target class |
| Behavior flag | A named behavior that a target turns on | price-in-meta |
| Adapter | New logic for one target or one class | A second request for the price |
| General rule | A change that is correct for each target | A parser that accepts a comma and a point |
data first
A changed value goes into data, never into code. The data file has a schema and a version. Thus a change of a value is a change of data, and a test can read the file.
An override is such a data entry. It changes one value for one scope.
Each override holds a behavior ID, a scope, a value, an owner, a reason, an evidence reference, an expiry date, and a hit counter. For example, an override sets the decimal mark to a comma for one target class, with an expiry at the end of the quarter.
ask about a behavior, not about a target
A behavior flag names what a target does. The general code asks about the flag:
- Wrong:
if (target === "lab-target-b"). - Correct:
if (flags.has("price-in-meta")).
The first form grows with each target, and nobody can see why each branch exists. The second form has one branch for each behavior.
A new target with the same behavior gets one data row and no code change. For example, a second target with the price in a meta element gets the flag price-in-meta, and the code does not change.
adapters for new logic
New logic, for example a second request for a price, goes into an adapter. The adapter is behind a small contract. Its mandatory outputs must be present, or the record fails. Its optional outputs can be absent, and they fail softly with an empty field. The full contract of an adapter is the site adapter contract of The adapter contract and templates by target class.
one effective value
Two or more overrides can match one request. The override precedence gives one effective value. I use five scopes, from the weakest to the strongest: default, target class, target, path, and run. The most specific scope that matches wins.
For example, the class override sets a comma, and the target override sets a point. A request to that target gets the point. A request to a different target of the class gets the comma.
the generic fallback
The generic fallback is the last adapter. It tries general methods. When it finds nothing, it returns an explicit unsupported result. It never returns a partial record as a success, because a weak success hides the edge case from each count and each alarm.
For example, a page has no price that the general methods can find. The fallback returns unsupported with the reason "no price". The record then goes to the queue of unknown cases, not to the customer.
the two sides
| automation side | detection side |
|---|---|
| A behavior table keeps the general code clean. A branch on a target name grows until a fix for one target breaks a different target. Overrides as data, with one precedence order, give one effective value that a test can check. | A detection rule set has the same places: a general rule, an exception for one environment, and a threshold. Tune a rule that is too broad for all traffic. Add an exception only for one known-safe source. |
design decisions
| option | gain | cost | when it is correct |
|---|---|---|---|
| A branch on a target name | Fast to write | It grows with each target, and a fix for one target can break a different target. | Never in general code |
| A data row with a schema | A value change is not a code change. | The schema must hold the value. | For a changed value |
| A behavior flag | One branch for each behavior, not for each target | Someone must name the behavior. | When two or more targets can show the behavior |
| An adapter behind a contract | New logic stays out of the general code. | Each adapter is code to keep. | For new logic for one target or one class |
| One precedence order | One effective value for each request | All overrides must use the same scopes. | Always, when overrides exist |
A generic fallback with unsupported | Each failure is visible and counted. | The customer sees an explicit gap. | Always, as the last adapter |
diagram
The first diagram shows the places of a special case. The second diagram shows the precedence order.
where a special case lives, from the cheapest place to the last result
- Data rowA changed value, for example a date format, in a data file with a schema and a version.
- Behavior flagA named behavior that a target turns on. General code asks about the behavior, not about the target.
- AdapterNew logic for one target or class, behind a small contract with mandatory outputs and optional outputs that fail softly.
- General ruleA change of the general code that is correct for each target of the class.
- Generic fallbackThe last adapter. It tries general methods and returns an explicit unsupported result when it finds nothing.
One record, or an explicit unsupported result, never a weak successpass
the precedence stack: the most specific matching scope gives the effective value
- RunAn override for one run only. It wins over each scope below it.
- PathAn override for one path form, for example the archive pages.
- TargetAn override for one target.
- Target classAn override for each target of one class.
- DefaultThe value of the general code when no override matches.
One effective value for each behavior and each requestpass
code example
The example holds four synthetic overrides of one behavior, a resolver, and an expiry test. All target names are lab names. The evidence field holds the ID of a stored capture.
An override schema with its fields, a resolver for the effective value, and a test that fails after the expiry.
// Overrides as data: a schema, a resolver, and an expiry test. All names are lab names.
type Scope = "default" | "class" | "target" | "path" | "run";
const PRECEDENCE: Scope[] = ["default", "class", "target", "path", "run"]; // later wins
type Override = {
behavior: string; // behavior ID, for example "decimal-mark"
scope: Scope;
match: string; // the class, target, path, or run that the scope names
value: string;
owner: string;
reason: string;
evidence: string; // ID of the stored capture that shows the behavior
expires: string; // ISO date
hits: number; // hit counter: times that the override changed a result
};
type Request = { targetClass: string; target: string; path: string; run: string };
function resolve(all: Override[], behavior: string, req: Request): Override | undefined {
const key: Record<Scope, string> = { default: "*", class: req.targetClass, target: req.target, path: req.path, run: req.run };
const matches = all.filter((o) => o.behavior === behavior && o.match === key[o.scope]);
matches.sort((a, b) => PRECEDENCE.indexOf(a.scope) - PRECEDENCE.indexOf(b.scope));
return matches.at(-1);
}
// The test fails for each override that is past its expiry date.
function expiryTest(all: Override[], today: string): string[] {
return all.filter((o) => o.expires <= today).map((o) => `${o.behavior}/${o.scope}/${o.match} expired on ${o.expires} (owner ${o.owner})`);
}
const overrides: Override[] = [
{ behavior: "decimal-mark", scope: "default", match: "*", value: ".", owner: "team-a", reason: "general rule", evidence: "none", expires: "2099-01-01", hits: 0 },
{ behavior: "decimal-mark", scope: "class", match: "shop-eu", value: ",", owner: "team-a", reason: "class uses a comma", evidence: "cap-101", expires: "2026-09-30", hits: 412 },
{ behavior: "decimal-mark", scope: "target", match: "lab-target-b", value: ".", owner: "team-b", reason: "target uses a point", evidence: "cap-117", expires: "2026-05-31", hits: 3 },
{ behavior: "decimal-mark", scope: "path", match: "/archive", value: ",", owner: "team-b", reason: "old pages", evidence: "cap-120", expires: "2026-12-31", hits: 0 },
];
const today = process.argv[2] ?? "2026-06-01";
const requests: Request[] = [
{ targetClass: "shop-eu", target: "lab-target-a", path: "/item", run: "r7" },
{ targetClass: "shop-eu", target: "lab-target-b", path: "/item", run: "r7" },
{ targetClass: "shop-eu", target: "lab-target-b", path: "/archive", run: "r7" },
{ targetClass: "shop-na", target: "lab-target-c", path: "/item", run: "r7" },
];
for (const r of requests) {
const o = resolve(overrides, "decimal-mark", r);
console.log(`${r.target}${r.path}: "${o?.value}" from scope ${o?.scope}`);
}
const failed = expiryTest(overrides, today);
console.log(`expiry test on ${today}: ${failed.length === 0 ? "pass" : "fail"}`);
for (const f of failed) console.log(` ${f}`);output
The effective value and its scope for four requests, and the result of the expiry test.
lab-target-a/item: "," from scope class
lab-target-b/item: "." from scope target
lab-target-b/archive: "," from scope path
lab-target-c/item: "." from scope default
expiry test on 2026-06-01: fail
decimal-mark/target/lab-target-b expired on 2026-05-31 (owner team-b)The output shows that each request gets one value from the most specific scope that matches. The expiry test fails for the target override, and it names the owner who must renew or remove it.
data
block signature data in an early and a late period
status of this figure: documented in my test system
source: Benchmark platform, code (my test system): block signature data, early and late; date 2026-11-16; sample of 5; condition: The n is the count of changes after the first build of the block signatures. The middle period has one value only (5 steps) and stays out.
data table
| series | kind of item | items |
|---|---|---|
| early | marker entries | 14 |
| early | vendor rows | 6 |
| early | marker groups | 1 |
| early | ordered steps | 4 |
| late | marker entries | 14 |
| late | vendor rows | 6 |
| late | marker groups | 3 |
| late | ordered steps | 6 |
The first chart comes from the code of the benchmark platform, my test system. The special cases of one component moved into data with more structure, and the count of entries did not grow.
f-scores of four generic content extractors on one public evaluation set
status of this figure: from a public source
source: Public evaluation page of an open-source content extractor (public source, https://trafilatura.readthedocs.io/en/latest/evaluation.html, read 2026-11-12); date 2026-11-12; sample of 990; condition: One mixed evaluation set of documents. The numbers are the F-scores that the page gives for the generic path.
data table
| extractor | F-score |
|---|---|
| extractor 1 | 0.926 |
| extractor 2 | 0.853 |
| extractor 3 | 0.807 |
| extractor 4 | 0.803 |
No generic extractor in the second chart gets a full score. Thus the general path must have a generic fallback with an explicit result for the pages that it cannot read.
failure modes
| problem | cause | correction |
|---|---|---|
| A fix for one target breaks a different target. | The general code branches on target names. | Move the value into data, or name the behavior with a flag. |
| Two overrides give two values for one request. | The overrides have no precedence order. | Use one order of scopes, and let the most specific scope win. |
| An override stays after its cause goes away. | The override has no owner and no expiry. | Give each override an owner and an expiry, and run the expiry test. |
| The customer gets records with empty mandatory fields. | The fallback returns a partial record as a success. | Return an explicit unsupported result from the generic fallback. |
| An adapter holds a changed value only. | The team put a data change in code. | Put a changed value in a data row, and keep adapters for new logic. |
check questions
A target writes its dates with the day first. Each other target of its class writes them with the year first. Where does this special case go?
- A branch on the target name in the general code
- A new adapter for the target
- A data row or an override for the target
- The generic fallback
answer
c. A data row or an override for the target
A changed value goes into data, never into code. An adapter is for new logic, and a branch on a target name grows with each target.
List the fields of an override, and say why it has an expiry and a hit counter.
sample answer
An override holds a behavior ID, a scope, a value, an owner, a reason, an evidence reference, an expiry date, and a hit counter. The expiry makes the owner renew or remove the override. The hit counter shows if the override still changes results.
A class override sets a comma as the decimal mark. A path override for the archive pages sets a point. A request goes to an archive page of a target of that class. What is the effective value?
- A point, because the path scope is more specific than the class scope
- A comma, because the class override comes first
- No value, because two overrides match
answer
a. A point, because the path scope is more specific than the class scope
The precedence order gives one effective value. The most specific scope that matches wins, and the path scope is more specific than the target class.
Explain why the generic fallback returns an explicit unsupported result when it finds nothing.
sample answer
A partial record that passes as a success hides the edge case from each count and each alarm. The customer then gets empty fields with no warning. An explicit unsupported result is visible and counted, and the record goes to the queue of unknown cases.
exercise
exercise Do this task on paper with the synthetic overrides and requests below. Today is 2026-06-01. The precedence order is default, target class, target, path, run: the most specific scope that matches wins.
| override | scope | match | value of the date order | expiry |
|---|---|---|---|---|
| 1 | default | each request | year first | 2099-01-01 |
| 2 | target class | news-eu | day first | 2026-12-31 |
| 3 | target | lab-target-d | month first | 2026-04-30 |
| 4 | run | run r9 | year first | 2026-07-01 |
| request | target class | target | run |
|---|---|---|---|
| A | news-eu | lab-target-e | r8 |
| B | news-eu | lab-target-d | r8 |
| C | news-na | lab-target-f | r9 |
- Find the overrides that match each request.
- Write the effective value of each request.
- Write the overrides that fail the expiry test today.
sample answer
The answer uses the four overrides and the three requests of the task.
Request A matches overrides 1 and 2, and the class scope wins: day first. Request B matches overrides 1, 2, and 3, and the target scope wins: month first. Request C matches overrides 1 and 4, and the run scope wins: year first.
Override 3 expired on 2026-04-30, thus it fails the expiry test today. Its owner must renew it with new evidence or remove it.
sources
- My model of the places of a special case, of override precedence, and of the generic fallback, 2026-11 (not public).