Invariants

Understand invariants and how they function to make the properties stronger and more reliable.

What are invariants?

Some programs and functions are complex to describe and reason about. They might need many small parts to work correctly to be functional, or we may not be able to assert their quality because it’s difficult to define. For example, the judgment about whether a meal is good is subjective, although it could include criteria. The criteria could include whether the ingredients were cooked adequately; whether is served at the correct temperature; whether the meal is too salty, sweet, or bitter; etc. These factors play a part in the decision of “whether the food is good or not.”

Similarly, in a software system, we can identify similar conditions that should always remain true. These are called invariants.

Advantages of invariants

Testing with invariants is a great way to get around the fact ...