Question

I am wondering if there is a definitive procedure in determining invariant for a specific class or does it change on a case by case basis?

Obviously it depends on certain variables and require statements but my question is,

Is there some sort of guide I can following to determine the best invariants for a specific class?

Thanks

Was it helpful?

Solution

Design by contract concepts get slightly complicated when they are adapted to object-oriented languages.

A class invariant is a property that each instance of the class is guaranteed to have when a method is called (like a common pre-condition for all methods), and that in return each method and constructor must ensure remains true when they terminate (like a common post-condition).

They are good for expressing consistency conditions. A Wallet class that modelizes an actual wallet might have the class invariant that the amount contained is always positive.

Class invariants, like the rest of the contract, are inherited. New implementations of methods must provide the same guarantees as the methods they replace.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top