index/engineering/burden-of-proof/Correct According to What?
engineeringburden-of-proof

Correct According to What?

I was asked to verify a complex SQL query. 'Correct' had no shared definition — nothing to verify against. Review alone could never settle it.

tags: testing, risk

A query landed in front of me in the middle of an ordinary feature task. It was SQL that re-expressed application logic that had lived in another language for years: multiple CTEs chaining transformations, temporal conditions deciding which version of a record was current on a given date, business rules applied in sequence. It came with a one-line request.

Can you verify this is correct?

The request should have been straightforward. It wasn’t, and it took me longer than I’d like to admit why: the problem was never the SQL.

Any Language, Same Question

Take the same logic — the transformations, the temporal rules, the ordering of decisions — and write it in Node.js, Python, or Java. Hand it over with the same question, and the review stalls in the same place. SQL was simply the medium the logic happened to arrive in.

An implementation that encodes decisions also encodes an obligation to demonstrate them. The language changes. The responsibility to demonstrate behavior does not. That responsibility was the part missing from the request — and it was missing before a single line was read.

“Looks Correct” Is Not “Is Correct”

Code review answers one question well: does this look right? A reviewer reads for suspicious patterns, off-by-one edges, ordering mistakes — the failures that announce themselves to a careful reader. That’s real work, and it’s worth doing. But it is not the same question as is this right?, and the distance between the two opens exactly where logic gets interesting: when rules interact, when “current” depends on a date, when a later step quietly assumes what an earlier step produced.

A query like this one can look entirely reasonable and still be wrong in ways no amount of reading will surface — because the wrongness lives in behavior, and reading is not executing.

Review catches what looks wrong. Verification establishes what is actually produced. The request asked for the second while supplying only what the first requires.

The Circular Verification Problem

What made the request unanswerable wasn’t the absence of a test suite. It was the shape of the evidence on offer:

How do we know what it's supposed to do?
        ↓
Read the implementation

How do we know the implementation is right?
        ↓
Read the implementation more carefully

The implementation was its own reference. To check a claim you need the claim stated somewhere other than the thing being checked — and nothing in the request pointed at such a place. Without one, “verify this is correct” quietly becomes “convince yourself it’s correct,” which is a different and much weaker request.

What Was Missing Was Not “Tests”

The obvious next question: so you wanted unit tests? No — and the distinction matters, because “add tests” is a fix for the wrong problem.

A framework wasn’t the gap. The gap was the absence of any statement, in any form, of what the query was supposed to produce: conditions in, expected behavior out. Structured scenarios with expected results. Controlled data with known answers. A specification. Even a worked example would have helped — anything that lets the implementation be checked against an expectation that didn’t come from the implementation.

The format is secondary. The property is not: expected behavior has to exist independently of the code that implements it. That is what turns verification from self-confirmation into a check.

What Stayed Behind

The original code did have that. It was checked at two levels that complemented each other: unit tests for the pure logic, and isolated tests that ran its queries against a local database stocked with data. The first level caught decisions that were wrong on their own. The second caught what pure logic can’t: how a query actually behaves against data — NULL handling, date semantics, the edges where SQL quietly disagrees with the application code around it. Two levels, because neither covers the other’s ground. What mattered wasn’t the tests themselves but what they carried: expectations stated apart from the code, which is what made “correct” checkable.

That structure didn’t survive the rewrite. Logic that used to sit in testable functions now sits in query steps — there’s no function boundary left to unit-test.

Yes… I know, SQL can be tested, and the old tests may even have fixtures worth reusing. That was never the obstacle. Nothing came with the request to show the author had verified it, and nothing said how far my verification was supposed to go: equivalence with the old logic, the temporal edges, performance, all of it? Whatever I chose to check would become my definition of correct, and the evidence would be mine instead of theirs. The levels that made “correct” checkable for the original stayed behind with it. The word made the trip alone.

Real Logic Makes Mental Execution Fail Faster

Why does this bite harder here than it would for, say, a CRUD endpoint? Because the reviewer’s private simulation of the code — mental execution — degrades fast as interacting steps accumulate. Transform, window by date, apply a rule, transform again, aggregate. Each step is easy to follow alone. The problem is the compounding: step five silently depends on a decision step two made, and the whole chain has to be held in one head at once.

Temporal conditions are the worst case. “Which row is current” is not a fact you can see in the query; it is the output of a comparison against a date that itself came from an earlier step. The moment conditions depend on data instead of literals, eyeballing stops being a verification method and becomes a hope.

None of this argues that people can’t review such code — they can, and should. It argues that review has limits, and this query had sailed past them by design. The more logic an implementation encodes, the less reliable reading it becomes, and the more it needs that independent statement of expectations.

The Request Was Already Evidence

The request is evidence of one thing: when verification entered the process. Here is the part that matters even after all the missing structure gets supplied. Say the team comes back next week with scenarios, expected results, a harness — the whole thing. The original request still happened. Improvements don’t rewrite history; they only come after it. What the request records is fixed: the ask for verification arrived at handover — after design, after implementation, at the moment someone needed to trust the query.

That is not an accusation. The original’s two levels of tests verified the original — verification doesn’t transfer to a rewrite just because the rewrite came from it. Maybe porting the tests was planned for a later stage; maybe the new shape made it unclear how. The incident only proves what was available at the moment independent verification was requested — and what was available was an implementation asking to be checked against nothing.

“Here is how we verified it” is a different sentence from “can you verify this?” One shows the burden was carried. The other asks someone to pick it up.

And that burden belongs to the author. Only the person who wrote the logic knows what it was meant to do: the intent, the cases they had in mind, the edges they were careful about. For them, writing a few of those down is cheap. For anyone arriving later, reconstructing them from the implementation is the entire job, and what comes out is a guess. A reviewer can check expectations. Only the author can supply them.

Correct According to What?

The question in the title can’t be answered by reading. It has to be answered before the reading, by the side that wrote the query.

The answer doesn’t have to be elaborate. A plain, explicit assertion — the sort you’d read in a test report — is enough: should return 200 total transactions when date = 2025-01-01, status = active and discount <= 5.0. That sentence is shared understanding of what to verify against. When it doesn’t travel with the implementation, the reviewer doesn’t review: they guess what correct means, then check against the guess in private. The request wasn’t missing tests, and it wasn’t missing a sharper reviewer. It was missing the sentence that told both sides what “correct” meant.

There is an obvious way to verify a conversion like this one — run the old implementation against the new and compare — and it doesn’t rescue this story, it relocates it. It makes the work someone’s job, which raises the question of whose. That’s part two.