HowToUseCairngormContract

How to use Cairngorm Design by Contract

Summary

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.

Introduction

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.

Usage

Create boolean expressions either as precondition or postcondition:

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

    parameters.unshift(errorCode);

    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.

References

Martin, P. (March 2010). Design by Contract. Retrieved May 10, 2010 from http://blogs.adobe.com/pmartin/archives/2010/03/design_by_contr.html.

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]