Section 7g

Programming by Contract - Post Conditions


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Programming by Contract:

Post Conditions

The next step is to figure out what should have happened when the method has completed its mission. This is not any more difficult than preconditions. Basically, you figure out what the resulting value to be returned (if any) should be.

For the previous examples, divideTwoNumbers should return the quotient result of the divided numbers, calcFactorial should return a factorial, and calcTotal should calculate a tax from the subtotal, add the tax to the subtotal, and then return it. Your postconditions for all normal circumstances should simply be the reason you wrote the method.

Note that some methods that conduct I/O will rarely if ever return a result. However, the postconditions for this kind of method will be that a certain thing is displayed on the screen, some data is output to a file, and so on.

This is also a good place to note the difference between processing and I/O methods. A processing method is designed to take in some data, conduct some kind of processing such as math or decision making, and return a result to the calling method. An I/O method is a method that delivers data to a device. Your iostream methods deliver data from your program to the monitor as did your printString or printChar methods. Your processing methods calculated the roots of a quadratic equation or the remaining side of a right triangle.

Processing and I/O methods must not be combined. It is very common to see methods that calculate something and then output the result on the Internet and in many textbooks. This represents very poor -- in fact, virtually nonexistant -- modularity, and obviously very poor software development. If you conduct output within a processing module, you will almost certainly have to go back and change the appearance of the output, or even change the tools of the output if you used a particular Java class or utility, the formatted command line I/O tools, or any of a variety of other output operations. Again, this means the method lacks the modulariy required to lift it out of your program and drop it into someone else's program who needs the kind of processing conducted in the method. Again, do not combine I/O and processing operations in the same method.

Like the preconditions, once you know what the postconditions are, you can decide on (and specify) the output of the method. Generally, the output of the method is the value that the method returns from itself. However, other output processes may involve display on the screen, output to a file, or other output actions, as mentioned previously.

When you begin work with the Six Step Programming Process, you will select what kind of output the method has, whether it is to return some processed quantity, or to send some data to a device. In any event, the reason to specify post conditions is so you can verify that your method does in fact do what it was meant to do. This is not a long or complicated section, and identifying post conditions is not difficult. However, as you will find, it will be critical to your program development.