Programming by Contract

Student: Alicia Rohrberg

Topic: Design by Contract (known often as Programming by Contract)

Email: arohrber@calpoly.edu

Key Points:

Presentation

What is Programming by Contract

Programming by Contract is a software correctness methodology. By using pre and post conditions, you can document the change in state caused by that piece of the program. Originally proposed by Bertrand Meyer who is the pioneering designer of the programming language, Eiffel. Methods are viewed as a way to fulfill a contract.

Benefits of Programming by Contract

Doing Programming by Contract

When a developer is following "Programming by Contract" the following questions must constantly be asked ...

Programming by Contract can be followed by implementing the sections that will be discussed in the rest of the presentation.

Using Preconditions

Using Postconditions

Using Assertions

Using Class Invariants

Exceptions in the Contract

Exam Questions

Question #1. What are the three central ideas of Programming by Contract?

Solution #1: Preconditions, Postconditions (assertions), and Class Invariants

Question #2: What should you do if a postcondition fails?

Solution #2: Go back through your testing phases and use assertions to find where the method is failing. Once the assertion passes the postcondition will pass.

References: