Skip to content

Contracts Overview

KuSpa edited this page Feb 16, 2018 · 6 revisions

What are Contracts?

Contracts are a way to monitor and test the integrity of your system at runtime. If used properly, they can reduce the time between the corruption of your system's state and a resulting error to a minimum or even catch the corruption. That makes it a lot easier to debug and maintain your system.

How do Contracts work?

Contracts are installed as Wrapper of CompiledMethods in the methodDict of a Class. Whenever the CompiledMethod would be executed, its Contract is executed instead.
A Contract can test the input parameters, with which the method is called. When they do not match the type of input parameter under which the Method guarantees to work correctly, the Contract throws an Exception and stops the execution. The test is defined in the Precondition.
After that, the Contract tests, if the Object is in a proper state and is not defect. This is defined in the Invariant of an Objects class. An Object has to fit the Invariant at all time. After guaranteeing a correct Objectsstate and correct input parameters, the method is executed. Then, the Contract tests with a Postcondition, if the method executed properly and again tests if the Object still fits the Classes Invariant.

When are Contracts executed?

A full Contract is ONLY executed when the Method call is not sent by an other Contract. The Precondition is tested on every method call, because it has no side effects.

Otherwise, if a Contract is executed, when the sender was the Object itself, it can happen that the Invariant triggered the method call itself. The Contract of the called Method would test the Invariant again, which would lead to another method call and so on. To prevent this endless recursion, a complete execution of a Contract is done only on extern calls.

How to use Contracts?

Contracts are managed via the ContractsBrowser.

Clone this wiki locally