I first read about Design by Contract in the book “Object-Orientated Software Construction by Bertrand Meyer”:http://www.amazon.co.uk/Object-oriented-Software-Construction-Prentice-Hall-Resource/dp/0136291554/ref=sr_1_1?ie=UTF8&s=books&qid=1219866553&sr=8-1. 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.”_
I also want to cite another great book that has gained favor with many of my peers, “Clean Code: A Handbook of Agile Software Craftmanship by Robert C. Martin”:http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882/ref=sr_1_1?ie=UTF8&s=books&qid=1267554535&sr=8-1. Chapter 7 discusses Error Handling and promotes that we don’t return null from methods and nor should we pass null to methods as both invite errors. As it says in the book _”all it takes is one missing null check to send an application spinning out of control”_.
I first used Design by Contract in the late 90′s and the results were dramatic. We saw a big improvement in code quality as we were thinking about the contract between a method and its caller. In the Java world we now have a number of mature Design by Contract libraries such as Contract4J. Take a look at it discusses the theory in more detail than I am.
A while back I created an over-simplified library for ActionScript. It’s really, really simple. There is one main class, Contract, that you can use to assert pre and post-conditions.
It works off 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.
It’s simple but effective. Download it “here”:http://blogs.adobe.com/pmartin/code/Contract.zip.