design by contract : Java Glossary

go to home page D words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish by Roedy Green ©1996-2008 Canadian Mind Products
index page for letter ⇒ punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
design by contract
A programming technique most commonly used in Eiffel to pepper your code with assertions. These define a contract on each method — the preconditions the caller must guarantee, and the postconditions the callee guarantees.
“One should not write a class without a specification — a contract. The contract lists the internal consistency conditions that the class will maintain (the invariant) and, for each operation, the correctness conditions that are the responsibility of the client (the precondition) and those which the operation promises to establish in return (the postcondition).

Writing a class without its contract would be similar to producing an engineering component (electrical circuit, VLSI chip, bridge, engine…) without a spec. No professional engineer would even consider the idea.”
~ Bertrand Meyer, Description of Design By Contract

book cover recommend book⇒Object-Oriented Software Construction
 paperbackhardcover
ISBN13:978-0-13-629155-8clickcounter978-0-13-629049-0clickcounter
ISBN10:0-13-629155-4clickcounter0-13-629049-3clickcounter
publisher:Prentice Hall
published:2000-03-21
by:Bertrand Meyer
This is the classic text on object-oriented programming. This is the best-written computer textbook on any subject I have yet encountered. If you ever plan to write a book, look at this to see how it should be done. He uses the Eiffel language for his examples. This could be confusing if you don’t already have little Java under your belt. This is not a book on coding, but rather on the general object-oriented principles behind the languages.
UK flag abe books.co.uk abe books.ca Canadian flag
UK flag amazon.co.uk. amazon.ca. Canadian flag
German flag abe books.de chapters.indigo.ca . Canadian flag
German flag amazon.de. abe books.com American flag
French flag abe books.fr amazon.com. American flag
French flag amazon.fr. barnes and noble.com American flag
Italian flag abe books.it powells.com American flag
Spanish flag iberlibro.com abe books anz Australian flag

book cover recommend book⇒Design By Contract
 paperback
ISBN13:978-0-13-088921-8clickcounter
ISBN10:0-13-088921-0clickcounter
publisher:Prentice Hall
published:2001-06-15
by:Bertrand Meyer
Difficult to find. Try Chapters.
UK flag abe books.co.uk abe books.ca Canadian flag
UK flag amazon.co.uk. amazon.ca. Canadian flag
German flag abe books.de chapters.indigo.ca . Canadian flag
German flag amazon.de. abe books.com American flag
French flag abe books.fr amazon.com. American flag
French flag amazon.fr. barnes and noble.com American flag
Italian flag abe books.it powells.com American flag
Spanish flag iberlibro.com abe books anz Australian flag
There are class invariants, which are conditions about overall class consistency that must be true any time any method completes. The official definition of a class invariant is an assertion which must be satisfied on creation of every instance of a class, and preserved by every exported (public) routine of the class, so that it will be satisfied by all instances of the class whenever they are externally observable. (I’m not sure how they handle multithreading.)

You can also have invariants on the non-public parts. Only the invariants involving purely public features (methods and variables) are in themselves public.

Eiffel lets you talk about both the old and new values of variables in your assertions. In Eiffel, these conditions are a formal part of the language and may optionally be checked at run time. The beautiful part is that the contract is inherited. A subclass may have a tighter, but not looser contract. None of the Java assertion schemes can do this.

Speaking more precisely, in a subclass, preconditions for an inherited method can only be equal to or weaker than that of the original precondition; and postconditions for an inherited method can only be equal to or stronger than that of the original postcondition. In other words, the precondition cannot be modified in the subclass to require more from the client than what is required in the parent class.

Another form of assertion is called the loop invariant, something that must be true each time through a loop. These assertions are not considered part of a method’s or class’s contract with the rest of the world, however. Similarly, there are assertions to ensure a loop eventually terminates.

In Java there are various ways of kludging design by contract and assertions including: iContract, JMSAssert, jContractor, Handshake, Jass, JML and JPP. Starting with Java 1.4, Java has a weak built-in assertions mechanism. It is a far cry from design by contract.

Learning More

Sun RFE number 4449383 : JSR to add Design By Contract to Java

CMP homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.61] The information on this page is for non-military use only.
You are visitor number 9,715. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/designbycontract.html J:\mindprod\jgloss\designbycontract.html