Invariant

This note has been superseded by [[20200730082558]].

A condition that must always be true.

Often refers to a condition which aren't explicitly held true by the structure of data. For example, an array will keep elements in a definite order, such that if a appears earlier than b, and b appears earlier than c, than a appears earlier than c, referred to as transitivity. If, however, a programmer were to create their own linked list structure, they would need to protect the invariant of transitivity themselves. Conversely, if the programmer created a set of elements that maintained their order with an numerical index property on the object, they would know that transitivity was guaranteed by the transitivity of numbers.

Explicit invariants are a great opportunity to drop to a lower level of abstraction, and to create a generic data-structure whose sole purpose is to collect items and guarantee the invariants.

An invariant is any logical rule that must be obeyed throughout the execution of your program that can be communicated to a human, but not to your compiler.

From https://softwareengineering.stackexchange.com/questions/32727/what-are-invariants-how-can-they-be-used-and-have-you-ever-used-it-in-your-pro

#software