64
invariant
A fact that must stay true. Balance not negative. A child row never outlives its parent.
What is invariant?
An invariant is a condition the system must hold before and after every change. Types can encode some. Databases encode others. Tests encode the rest. If it can be false, it is not an invariant yet — it is a hope.
Why does invariant matter when vibe coding?
Models implement the happy update and leave the illegal state representable. Name the invariant and you get a constraint, not a comment.
How do you do invariant?
For each object, write the sentences that must always be true. Put each in the strongest place: type, check constraint, or test. Reject “we’ll validate in the UI.”
How do you ask a model for invariant?
Enforce this invariant: (fact). Prefer the database or the type. A UI check is not enough. Add a test that tries to break it.
What goes wrong with invariant?
An invariant written only in a docstring. The model will not read it next week. The database will.