How to use Cairngorm Design by Contract


The Design by Contract library allows to assert pre and post-conditions, following the idea of Design by Contract. Its use of conditional compilation ensures that production code remains unaffected.


Object-Orientated Software Construction by Bertrand Meyer. Chapter 11 discusses Design by Contract: building reliable software:

Assertions and the associated concepts, explained in this chapter, provide some of the answers. Although not foolproof, the mechanisms presented below provide the programmer with essential tools for expressing and validating correctness arguments. The key concept will be Design by Contract: viewing the relationship between a class and its clients as a formal agreement, expressing each party's rights and obligations. Only through such a precise definition of every module's claims and responsibilities can we hope to attain a significant degree of trust in large software systems.


Create boolean expressions either as precondition or postcondition:

private function getErrorMessage(errorCode:String, parameters:Array):String
    Contract.precondition(errorCode != null && errorCode.length > 0);


    var error:String=ErrorResourceUtil.getErrorString.apply(null, parameters);
    var codedError:String=StringUtil.substitute(CODED_ERROR, errorCode, error);

    Contract.postcondition(codedError != null && codedError.length > 0);

    return codedError;

The assertions are enabled with a compiler argument (-define=CONFIG::debugging,true ), for development set it to true and your assertions will be checked. For production, set it false and your assertions are not checked.


Martin, P. (March 2010). Design by Contract. Retrieved May 10, 2010 from

Meyer, B. (March 2000). Object-oriented Software Construction. Prentice-Hall Resource.

### Cairngorm 3 - [ Home ][1] - [ Guidelines ][3] - [ Tools ][4] - [ Libraries Downloads ][2]
### Cairngorm 2 - [ Home ][5] - [ Framework Downloads ][6] - [ Eclipse Plugin ][7]
### Project - [ Source ][8] - [ Bug Database ][9] - [ Submitting a Patch ][10] - [ Developer Documentation ][11] - [ Forums ][12] - [ License ][13]

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks