Notes
Slide Show
Outline
1
Preconditions and Postconditions
  • An important topic: preconditions and postconditions.
  • They are a method of specifying what a method accomplishes.
2
Preconditions and Postconditions
  • Frequently a programmer must communicate precisely what a method accomplishes, without any indication of how the method does its work.
3
Example
  • You are the head of a programming team and you want one of your programmers to write a method for  part of a project.
4
What are Preconditions and Postconditions?
  • One way to specify such requirements is with a pair of statements about the method.
  • The precondition statement indicates what must be true before the method is called.
  • The postcondition statement indicates what will be true when the method finishes its work.
5
Example
6
Example
  • The precondition and postcondition appear as comments in your program.
  • They are usually placed before the method’s implementation.
7
Example
  • In this example, the precondition requires that
  •             x  >=  0
  •     be true whenever the method is called.
8
Example
9
Example
10
Example
11
Example
  • The postcondition always indicates what work the method has accomplished.  In this case, when the method returns the square root of x has been written.
12
Another Example
13
Another Example
14
Another Example
15
Another Example
16
Always make sure the precondition is valid . . .
  • The programmer who calls the method is responsible for ensuring that the precondition is valid when the method is called.
17
. . . so the postcondition becomes true at the method’s end.
  • The programmer who writes the method counts on the precondition being valid, and ensures that the postcondition becomes true at the method’s end.
18
A Quiz
  • Suppose that you call a method, and you neglect to make sure that the precondition is valid.                                                      Who is responsible if this inadvertently causes a 40-day flood or other disaster?
  • You
  • The programmer who wrote that torrential method
  • Noah
19
A Quiz
  • Suppose that you call a method, and you neglect to make sure that the precondition is valid.                                                      Who is responsible if this inadvertently causes a 40-day flood or other disaster?
  • You
  •    The programmer who calls a method is responsible for ensuring that the precondition is valid.
20
On the other hand, careful programmers also follow these rules:
  • When you write a method, you should make every effort to detect when a precondition has been violated.
  • If you detect that a precondition has been violated, then print an error message and halt the program.
21
On the other hand, careful programmers also follow these rules:
  • When you write a method, you should make every effort to detect when a precondition has been violated.
  • If you detect that a precondition has been violated, then print an error message and halt the program...
  • ...rather than causing
  •     a disaster.
22
Example
  • Throwing an exception(described in Section 1.1) is useful.
23
Advantages of Using Preconditions and Postconditions
  • Succinctly describes the behavior of a method...
  • ... without cluttering up your thinking with details of how the method works.
  • At a later point, you may reimplement the method in a new way ...
  • ... but programs (which only depend on the precondition/postcondition) will still work with no changes.
24
Summary
  • Precondition
  • The programmer who calls a method ensures that the precondition is valid.
  • The programmer who writes a method can bank on the precondition being true when the method begins execution.
  • Postcondition
  • The programmer who writes a method ensures that the postcondition is true when the method finishes executing.
25
THE  END