An Object's Representation Invariant

A representation invariant is an internal property that the object's creation and supported operations keep true.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Choose a two-field example
  2. Reduce opportunities for disagreement

Choose a two-field example

A queue might store a list and a count. Its invariant says count equals the number of retained jobs. Inserting one must update both. Loop invariants use the same reasoning principle across steps.

Reduce opportunities for disagreement

If length can be obtained cheaply from the chosen structure, derive it instead of storing a separate mutable count. Test the empty result after removing the last item. Boundary cases reveal where update logic often differs.[1]

References

  1. Python Software Foundation, Python Tutorial: Classes ↩