Design by Contract(DbC) is a phrase coined by Bertrand Meyer, creator of the Eiffel programming language.

The basic concepts of DbC are preconditions, postconditions and class invariants. JJ is designed for beginning programming, but JJ also contains some advanced features, such as DbC, for instructors who wish to introduce these concepts early on.

Full featured Design by Contract is an invaluable tool for large software systems, particularly for reuse. Unfortunately, for full featured object oriented languages such as Eiffel and Java defining and implementing Design by Contract can be quite complex. Indeed, Design by Contract is not yet integrated into Java.

Fortunately for you, the JJ instructor or student, JJ is not a full featured object oriented programming language (there is no inheritance) so the Design by Contract definition is simple and straightforward. Fortunately, the same holds for us, the JJ designers and implementors: the JJ Design by Contract definition and implementation is simple and straightforward.

The JJ commands for Design by Contract are:

The precondition is specified in PreCheck commands. The postcondition is specified in PostCheck commands. The class invariant is specified in Check commands with the invariant construct (between the Invariant command and the EndInvariant command).

Is Design by Contract really that simple? For JJ, yes it is. There are a few rules that make this DbC definition work for JJ, rules that may be a little too restrictive for full featured object oriented langauges, but rules that are more than reasonable for a beginner's language.

  1. RULE: Functions are free of side effects.
  2. RULE: Private methods cannot directly call public methods in the same class.
For more on Design by Contract, please see the Eiffel programming language website.