Java: Design By Contract

Bertrand Meyer formalized 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:

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. See Assertions.


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