In the design by contracts, the class invariant must be satisfied on two occasions: after creating the object and after call a routine. Are there any examples or conditions, which I have to do the evaluation before the call to the routine too?
In the design by contracts, the class invariant must be satisfied on two occasions:
Share
The class invariant can be violated before a feature call. The conditions may be different, I’m presenting only the most obvious ones:
Aliasing. An object references some other object that is involved in a class invariant and that other object is modified by a third party:
Now the following code violates the invariant of class
SWITCH:External state. An object is coupled with external data that is referenced in a class invariant and may change unexpectedly:
Now if the database is modified outside the application, the cache may become inconsistent and a class invariant violation is triggered before the following feature call:
Callback. An object can be passed to the other one in an invalid state:
A callback to
HADNLER, performed by the featureiterate_over_elementsof the classSTRUCTURE, causes an invariant violation becausehandleris not in a good condition:One can argue that all the cases are because of software bugs and flaws, but this is exactly the purpose of class invariants to catch those including the cases when the violation happens before feature calls.