This chapter covers
- What contracts are, how to design and ensure them
- What pre-conditions, post-conditions, and invariants are and how to model them
Imagine a piece of software that handles a very complex financial process. For that "big routine" to happen, the software system chains calls to several sub-routines (or classes) in a complex flow of information, i.e., the results of one class are passed to the next class, so on and so forth. As usual, the data comes from different sources, such as databases, external web services, and the users themselves. At some point in the routine, the class TaxCalculator which, as its name says, handles the calculation of a specific tax, is called. From the requirements of this class, we see that the calculation only makes sense for positive numbers; a negative number would make the calculations go wild.
What we need to think about now is how we are going to model such a restriction. We know that values that come to TaxCalculator, whatever they are, must always be positive. I see three options when facing restrictions like that: