'

Simple Support for Design by Contract in C++

Pedro Guerreiro New University of Lisbon

Design by contract can be seen as an advanced software engineering technique for building quality software in a professional environment or as a fundamental programming con-cept, useful even for introductory programming. If design by contract is an afterthought, so-phisticated tool support, with macros, preprocessors or patterns is acceptable. If it is to be used from the very first programs, it must not be yet another difficult obstacle to the novice programmer. This point of view seems to recommend Eiffel as the sole vehicle for the early introduction of design by contract. However, compromises are possible, if your organization mandates C++, for example. For design by contract in C++ we use a class template, Assertions, which is inherited by the classes we are specifying. This class handles pre-conditions, postconditions and class invariants, and supports the “old” notation. The asser-tions themselves are not difficult to implement, but the “old” notation, which is necessary in order to compare the value of an attribute in a postcondition with its value at an earlier stage in the function, raises interesting issues. In most common situations, using the assertions is straightforward. There are, however, more rare cases involving inheritance and recursion that must be handled with a discipline. The system allows us to switch on and off the each type of assertions independently for each class, and to turn them off altogether at compile time (because of efficiency concerns). '