next up previous index
Next: Design Guidelines Up: Programming in Opus and Previous: Stochastic Unit Tests   Index


Programming by Contract

Several of the types of Opus objects (variables, models) have optional pre_check and post_check methods that allow you to use a ``programming by contract'' model of software development. In this technique a piece of code, such as a variable definition, can define a ``contract'' that specifies that if a given set of pre-conditions are met, the code is guaranteed to meet a given set of post-conditions. This ``contract'' can help (a) reduce debugging time by checking for invalid inputs or outputs, and (b) document the assumptions and behavior of the code.

Opus implements this programming by contract mechanism by automatically invoking the pre_check and post_check methods of variables, models, and simulations. This is done only if the Resources being used contain the ``check_variables'' key.

pre_check
This method for an Opus variable is called before the variable's compute method. This is a place to put optional tests of ``pre-conditions'' that this variable assumes in order to operate properly.

post_check
This method for an Opus variable is called after the variable's compute method. This is a place to put optional tests of ``post-conditions'' of what this Opus variable ``guarantees'' it will produce given that the ``pre-conditions'' tested in the pre_check passed.


next up previous index
Next: Design Guidelines Up: Programming in Opus and Previous: Stochastic Unit Tests   Index
info (at) urbansim.org