Representation Invariant

rep invariants = the bits of the data structure things that are always true; do not vary, unchanging

An object invariant, or representation invariant, is a computer programming construct consisting of a set of invariant properties that remain uncompromised regardless of the state of the object.

This ensures that the object will always meet predefined conditions, and that methods may, therefore, always reference the object without the risk of making inaccurate presumptions. Defining class invariants can help programmers and testers to catch more bugs during software testing.

In CS138, we saw that we can check the rep invariants with the isWellformed function during the pre-condition and post-condition.

Note that it’s OK to violate the invariant during operations. Else, how do you expect to make changes...just make sure the beginning and the end respect the invariant.