Search⌘ K
AI Features

Invariants

Explore how invariants define essential conditions in software that must always remain true. Understand their role in property-based testing to build reliable test suites by breaking down complex properties into simpler, verifiable parts.

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 that things may just be ambiguous otherwise. If an invariant were to be false at any time, we would know something isn’t functioning properly. Here are some examples

  • A store cannot sell
...