Blog · Craft · · 3 min read
Refusing defensive code
Mainstream software culture treats optional chaining (?.), || default, var(--x, fallback), and try/catch around internal data as signs of maturity. In Ptree they are treated like carpet over dirt: a way to let dirt fall below the standing surface so you do not have to look at it until the floor rots through.
Every defensive guard against an internal invariant causes two problems at once. First, it hides the exact moment that a bug entered the system. Second, it invents a synthetic fallback state that the rest of the program now has to deal with. If a tile that I created in the DOM is missing its <span>, or a taxon ID in cols[] fails to resolve, the right outcome is an immediate crash with a stack trace pointing at the broken line, not silently rendering an empty string and pretending that the page worked.
Validate at boundaries, trust everything inside
There is a sharp boundary between external reality and code that you own. External inputs break constantly, so boundaries are checked ruthlessly:
- Source files change shape without warning. Every capture declares the exact CSV header, spreadsheet columns, Parquet schema, or JSON keys that its provider must supply, and halts the build on any added, missing, renamed, or reordered column. Taxonomic standing tables likewise halt the capture the instant a provider invents a status word that we have not classified.
- User clicks can land anywhere on the page, so a delegated click handler checks
e.target.closest("li[data-id]"). - URL hashes come from the address bar, so the router validates every backbone, property token, view, filter expression, and coordinate against the permanent DOM registries before anything updates state.
Once a value crosses that boundary into the codebase, the guards stop. I built the DOM, I mutated the DOM, and I wrote the table that drives it. el.querySelector("span"), cols[+el.dataset.i].items.find(...), and a radio's .value dispatch into a module table all run without null checks. If one throws TypeError, I catch a real bug in five seconds instead of shipping a silent glitch.
That rule applies to broken links too. Early in development, an unresolvable shared taxon link fell back to the root view of Life so the screen would "look fine." That is the worst kind of defensive politeness. If a shared link breaks, I want the error loud and unmistakable rather than handing a teacher or researcher the wrong organism without warning.
Scope the query instead of guarding the result
Most null checks in front-end code are symptoms of a sloppy selector. If a tile is retracting out of the tree and should not be measured or anchored, the framework habit is to query every li[data-id] and scatter if (!el || el.classList.contains("leaving")) return across six callers.
Ptree scopes the query instead:
this._layer.querySelector(`li[data-id="${CSS.escape(id)}"]:not(.away,.leaving)`)
When a query asks only for valid elements, callers stop defending against ghosts. The same rule holds in CSS: var(--x, fallback) is forbidden on tokens that we define. If a custom property is misspelled or unset, a fallback masks the typo and paints a plausible wrong color for months. Leaving off the fallback makes the break obvious on the first glance.
The cost of "just in case"
Every ?. that you add invites the next reader, human or AI, to assume that the value can legitimately be undefined. Soon they add another branch downstream to handle the undefined that you invented, and half the file ends up defending against states that can never happen in a healthy run.
Fail early, fail often, and fail loudly. An honest crash on an impossible state takes a minute to diagnose and leaves the machinery clean. A polite fallback turns a one-line bug into a permanent mystery.