11.8. Guideline: How to Create and Write Contracts
Apply the following advice to create contracts:
- Identify system operations from the SSDs.
- For system operations that are complex and perhaps subtle in their results, or which are not clear in the use case, construct a contract.
- To describe the postconditions, use the following categories:
- instance creation and deletion
- attribute modification
- associations formed and broken
Writing Contracts
- As mentioned, write the postconditions in a declarative, passive past tense form (was …) to emphasize the observation of a change rather than a design of how it is going to be achieved. For example:
- (better) A SalesLineItem was created.
- (worse) Create a SalesLineItem .
- Remember to establish an association between existing objects or those newly created. For example, it is not enough that a new SalesLineItem instance is created when the enterItem operation occurs. After the operation is complete, it should also be true that the newly created instance was associated with Sale ; thus:
- The SalesLineItem was associated with the Sale (association formed).
What's the Most Common Mistake?
The most common problem is forgetting to include the forming of associations . Particularly when new instances are created, it is very likely that associations to several objects need be established. Don't forget!