A4.1 · Specification by Example · dive 2
Living documentation
When the worked examples are the checks the build runs, the documentation cannot drift from the system — a change that breaks an example breaks a check.
A prose document about behaviour and the code it describes are two sources that fork the moment one is edited and the other is not. A worked example is one source — the documentation a human reads and the check the build runs at once. Switch the view on the right to watch two sources drift while one source cannot.
Two sources, or one · switch the view
Documentation that is executed, not asserted
Ordinary documentation asserts. A page says “a request with no signed-in learner is redirected,” and the claim sits there whether or not the system still does it. The page and the code are two sources of the same truth, and the moment one is changed without the other, they fork. The document does not announce the fork; it goes on asserting the old behaviour, quietly wrong.
A worked example is documentation that is executed. It is the same artifact the build runs as a check, so it cannot assert a behaviour the system no longer has — if it did, the check would fail. There is one source, not two, so there is nothing to fork. The reader and the build read the same words, and the words are kept honest by being run.
- living documentation
- Documentation that is executed, not asserted. Because the example doubles as the check, the document and the system cannot diverge silently.
- drift
- The gap that opens between a static document and the system it describes, once they are two sources and only one is edited.
- one source
- The worked example is read by a human and run by the build. There is nothing to fork, so the documentation stays true.
- feedback edits the spec
- A behaviour change is made in the spec, and the example is regenerated — never patched in isolation, the rule that keeps the single source single.
This is the same single-source rule the two-layer model drew, applied to documentation: the spec is edited by feedback, and the examples it carries are regenerated — they are never edited apart from it.
Stale, not silently wrong
The value of living documentation shows the moment a behaviour changes. Take the three
/my/courses examples. Suppose someone weakens the not-signed-in behaviour from
“redirect” to “render whatever courses the request asked for”. With ordinary
documentation, the page that described the redirect would keep describing it — now false, and no one is
told, while the access rule is quietly broken. With living documentation, the not-signed-in example no longer
matches the system, so the check fails: the documentation announces its own staleness instead of lying.
Toggle the not-signed-in behaviour below. With the toggle off, all three examples match the system and the documentation is current. With it on, the not-signed-in example fails — the figure reports the document as stale, not silently wrong. That failure is the signal to edit the spec and regenerate the example, the proper repair.
Change the not-signed-in behaviour · watch the example flag it
A change that breaks an example breaks a check — the documentation reports itself stale instead of misleading the next reader.
On the Portal
On the Portal — the learning platform this course builds — the protected
/my/courses page carries its behaviour as three worked examples in its stories, settled
at F6.5 · Views with HEExThe HEEx rung that server-renders the catalog and reconciles the learner route into the protected /my/courses.View the spec ladder →, and the rung's Definition of Done restates the access outcome.
The page's documentation and its acceptance are the same lines: Portal.Enrollment over
an unauthenticated request redirects — never another learner's courses. There is no second prose source
to fall out of step.
The fragment below is spec text — the Definition-of-Done line paired with the matching Given/When/Then. If the not-signed-in behaviour changed, this example would fail; the repair is to edit the spec and regenerate, not to quietly correct the prose. Feedback edits the spec; the example follows.
# /my/courses — Definition of Done (F6.5): the documented outcome. # [ ] A request with no signed-in learner is redirected, # since /my/courses is protected — a learner never sees another's courses. # stories — the same outcome, as the executable check. # Given a request with no signed-in learner, # when GET /my/courses, # then it redirects — never another learner's courses. doc_line = "not signed in → redirected" # what the page says check_line = "not signed in → redirected" # what the build runs — the SAME line drift = :zero # one source; nothing to fork
The principle
Make the examples the specification and they become living documentation: executed, not asserted, so the document and the system cannot drift apart.
On the Portal
The /my/courses page's three Portal.Enrollment examples are
its documentation and its acceptance at once; a behaviour change fails the matching example, which is the
signal to edit the spec.
One source read by a human and run by the build cannot lie — if it disagreed with the system, the check would have failed.
Recap — documentation that cannot drift
A prose document and the code are two sources that fork when one is edited alone; a worked example is one source, read by a human and run by the build. Because it is executed, not asserted, it cannot describe a behaviour the system no longer has — a change that breaks the example breaks a check, and the documentation reports itself stale rather than lying. The repair is to edit the spec and regenerate. The closing dive names the mechanism beneath both moves: examples remove ambiguity, collapsing a requirement's readings from many to one.
Next in A4.1: A4.1.3 · Removing ambiguity — adding worked examples until exactly one reading of the requirement survives.
References
The sources behind living documentation, and where it connects in the course.
Sources
- Adzic, G. — Specification by Example — living documentation that cannot drift from the system.
- Gherkin reference — executable Given/When/Then that documents and checks at once.
- Cohn, M. — User Stories Applied — the acceptance that keeps a story honest.
Related in this course
- A4.1 · Specification by Example — the module hub.
- A4.1.1 · Examples as specification — the previous dive.
- A4.1.3 · Removing ambiguity — the next dive.
- A4 · The spec layer — the chapter.
- A4 · The spec ladder — the F6.5 rung this dive cites, across the iterations git recorded.
- A0.2.2 · The four artifacts — the spec the examples document.
- Companion · Phoenix (F6) — the real chapter whose F6.5 rung grounds this dive's /my/courses examples.