Six principles

In their book Design by Contract by Example Richard Mitchell and Jim McKim state six elementary principles how to write good contracts:


Principle 1: Separate queries from commands.
Queries return a result but do not change the visible properties of the object. Commands might change the object but do not return a result.


Principle 2: Separate basic queries from derived queries.
Derived queries can be specified in terms of basic queries.


Principle 3: For each derived query, write a postcondition that specifies what result will be returned, in terms of one or more basic queries.
Then, if we know the values of the basic queries, we also know the values of the derived queries.


Principle 4: For each command, write a postcondition that specifies the value of every basic query.
Now we know the total visible effect of each command.


Principle 5: For every query and command, decide on a suitable precondition.
Preconditions constrain when clients may call the queries and commands.


Principle 6: Write invariants to define unchanging properties of objects.
Concentrate on properties that help the reader build an appropriate conceptual model of the abstraction the class embodies.


These six principles have been applied to all the examples on this website and the tutorials and case studies.