Wednesday, May 6, 2009

What is Design By Contract

Design by Contract is a trademarked term of BertrandMeyer and implemented in his EiffelLanguage as assertions.
Design By Contract (DbC) is a software correctness methodology. It uses preconditions and postconditions (Contracts) to programmatically assert the change in state caused by a piece of a program.

The Preconditions and Postconditions that are in demand in DbC could summarized by the "three questions" that the designer must repeatedly ask:

    * What does it expect?
    * What does it guarantee?
    * What does it maintain?

Some attributes of Design By Contract...

Classes should specify their invariants: what is true before and after executing any public method.

Methods should specify their pre- and post-conditions: what must be true before and what must be true after their execution, respectively. Pre-conditions may be weakened by subclasses and post-conditions may be strengthened.

The explicit definition of these "assertions" forces the designer to think deeply about what is a requirement of the specification and what is an artifact of a particular implementation. These assertions can and should influence the declaration of "throws" exceptions as well as the throwing of runtime exceptions and try/catch in general.

For example if a method has specified some pre-condition then the failure of that condition is the responsibility of the client of the method. The method should not have to declare a checked exception for that condition, it should not have to test and throw for that condition, and likewise the client should not have to try/catch for that condition.

The client *should* do whatever is necessary to ensure it will meet the pre-conditions. It may do runtime tests or it may assume some condition is satisfied based on its own specification.

Many Languages supports DbC natively like  Cobra,Eiffel,Spec.

Java does not support DbC natively, but different tools are available in market which implements DbC.  Following lists some of the software that does this. Please visit the respective link for detail information.




No comments :

Post a Comment