Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

JVM Languages

Implementing Assertions for Java


Dr. Dobb's Journal January 1998: Design by Contract

Dr. Dobb's Journal January 1998

Design by Contract


Design by contract is a technique used to help ensure the correctness of software. A software contract is a specification of the behavior of a class and its associated methods. The contract outlines the responsibilities of both the caller (client) and the method being called. Failure to meet any of the responsibilities stated in the contract results in a breach of contract, and indicates the existence of a bug somewhere in the implementation of the software. Contracts increase the reusability and robustness of software while decreasing its complexity. Correctly implemented software contracts reduce the chance that software bugs could remain unnoticed during the testing of a program.

There are three elements essential to defining a software contract: preconditions, postconditions, and invariants. Preconditions and postconditions define the responsibility of the client and method, respectively. Invariants define rules common to both the client and the methods. Defining responsibilities for classes and methods helps you to avoid writing redundant checks because methods and classes are contractually bound to fulfill their part of the contract.

A method's preconditions represent the responsibilities that a client has when making a call to that method. Preconditions specify what a portion of a program's state must be at the entry of a method. If a method's preconditions are violated, then this is a breach of contract, and the method is freed from the responsibility of meeting its postconditions. It is the responsibility of the client to meet a method's preconditions, not the responsibility of the method to verify that its preconditions have been met.

Postconditions are used to specify a method's responsibilities. As long as its preconditions are satisfied, a method is responsible for leaving the program in a state defined by its postcondition. It is the method's responsibility to ensure that this state is reached, and the client can continue executing while safely assuming that the method has performed correctly.

Unlike preconditions and postconditions, invariants are universal across an entire class. An invariant specifies both the responsibilities that a client has for using a class, and the responsibilities that the class's methods have to the client. An invariant is equivalent to supplying a common condition as both a precondition and postcondition for each public method of a class. Meeting the requirements set by an invariant is the responsibility of the client when calling a method and the responsibility of the method before returning to the client.

A software contract can be completely specified through the use of preconditions, postconditions, and invariants. Specifying contracts in this manner produces classes that are easy to use and understand. The contract serves as a guide to using a class as well as a formal specification of the behavior of the class. Contracts are therefore useful during both the design and implementation phases of a project. Design by contract produces a class whose behavior is well defined and can therefore easily be reused in the future.

-- J.E.P., M.A.S., M.N.S.


Copyright © 1998, Dr. Dobb's Journal


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.