Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Design By Contract

JavaFAQ Home » Java Notes by Fred Swartz Go to all tips in Java Notes by Fred Swartz

Bookmark and Share Bertrand Meyer orginated a programming methodology called Design by Contract, which has become popular in some groups. In addition to specifying programming code to carry out the operations of a function (method), the programmer also specifies:
  • Preconditions - assumptions the function makes. These are usually expressed as statements that must be true about the parameters. This is the part of the "contract" that the caller must agree to.
  • Postconditions - conditions that are true when it finishes. These conditions define the responsibilities of the function, and are the part of the contract that the function agrees to.
  • Invariants - conditions that should be true of a class in general (class invariants. It's sometimes applied to loops (loop invariants). These seem to be generally less useful than pre- and postconditions.

Design benefits

The strength of this programming methodology is that it gets the programmer to think clearly about what a function does, and it provides documentation for the caller.

Programming language support for Design by Contract

A few programming languages, eg Eiffel and Sather Sather home page, implement pre- and postconditions in executable code so that they are checked at run time. Most programming language don't have such support, so programmers who want to use pre- and postconditions often write comments documenting the conditions. This is not an ideal situation because the comments aren't verified automatically, and they may not even be consistent with the actual code.


Perhaps the closest one can come in pure Java to implementing pre- and postconditions is to use the assert statement.


It seems that Bertrand Meyer's company, Interactive Software Engineering, has trademarked the phrase "Design by Contract".

Further reading

Building bug-free O-O software: An introduction to Design by Contract
 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search

Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy