CSC161 2011S Imperative Problem Solving

Reading: Design by Contract

Reading: Meyer: Applying Design by Contract. Available at http://se.ethz.ch/~meyer/publications/computer/contract.pdf.

Please submit a question by 9 p.m. on Tuesday, 8 March 2011.


Important Lessons

What is the contract mentioned on pages 3-5?

Weren't the Six-P's described to you as a contract? Essentially, the documentation for a procedure is a kind of contract: If the caller (the client) meets the preconditions, then the called function (the library) must ensure that the result meets the postconditions. I like thinking of it as a legal contract: If the caller fails to meet the preconditions, and something goes wrong, then it's the caller's fault. If the caller meets the preconditions, and something goes wrong, then it's the library writer's fault.

When you think of things this way, you often end up being a bit more aggressive in how you write your preconditions and postconditions. The client wants as few preconditions as possible (after all, it's a pain to have to check them). The client wants the code to do as much as possible. In contrast, the library writer wants to insert lots of preconditions to make sure that the code won't break in unpredictable ways, and wants as few postconditions as possible to make it easier to write the code. Getting the pair right is a balancing act.

Is there an instance where 'defensive programming" is not useful?

If your careful analysis suggests that nothing can go wrong, then defensive programming simply adds to the execution time. However, careful analysis is hard, so some defensive programming is often good.

In programming, what is the client and what is the supplier? I'm still a bit confused on this.

The supplier is the utility or library code (e.g., dpush and dpop). The client is the code that uses it (e.g., rpn).

What are invariants?

Invariants are guarantees you can make about the state of the variables in a program. We typically associate them with loops, and it's something we know is true at the start of every repetition of the loop. For example, in binary search, we know that if the value is in the array, then it's in the section bounded by the lower bound and upper bound.

Design by Contract would be considered a supplement to unit testing, right? Can you really do either without taking part in the other?

The two are independent. You can do unit testing without design-by-contract. That is, you can write tests as a way of specifying what you want your program components to do and as a way of ensuring that it does what you want to do without formally specifying preconditions and postconditions. Similarly, you can write a contract for each function (and maybe even do precondition testing) even if you don't do unit testing.


Object-Oriented Programming

I would like to understand the concept of object-oriented program a bit more. What does it mean to do object-oriented programming?

Could you please re-explain what object-oriented programming versus other programming is?

In functional programming (Scheme), we focus on writing functions and on using functions as data. In imperative programming, we focus on focus on sequencing instructions, many of which are designed to store values in memory or retrieve stored values. In object-oriented programming, we write programs as collections of communicating objects. An object is something that stores some values and provides some actions. For example, an object that represents a button in a computer program might keep track of how many times it's been pushed and might provide a respond to push action.

Why is reuse the corner stone of object-oriented programming?

Um ... it just is. OOP is used to solve two kinds of problems that programmers encounter. First, in modeling systems, it often makes sense to describe systems in terms of individual things that communicate with each other. For example, if we wanted to figure out the ideal number of cashiers to have on duty at any time at Wal-Mart, we might want to model customers and cashiers. Second, well-designed objects can be reused in a variety of ways: Directly, through inheritance, and through polymorphism.

What is "polymorphism" and how does it apply to "inheritance"?

Both are parts of object-oriented programming. Inheritance is the idea that once you've designed one kind of thing (e.g., a Calculator), when you want to build a variant, you can start with the existing thing and just extend it, without having to copy the code. Polymorphism is the idea that you can write general code that works with multiple types of parameters. (E.g., we could write a square function that works with integers or reals, or anything else we can mulitply.)


Annoying Answers

Are there any means of implementing inheritance/polymorphism in C?

Yes. However, we won't be doing them in this class.

Can you go over redefining a routine under contract?

No, I won't be in class. Maybe Andrew will.

The reading was way too long.

Why is the reading so long??? And why is it so hard to read? I have to highlight it...ok done ranting.

The reading is VERY TECHNICAL...i'm rereading this a couple more times.

Sorry. I want you to have the experience of reading different kinds of pieces on CS, including some pieces that you can't completely understand. (Other than K&R, of course.)

A lot of the concepts in the reading were explained by using examples of creating new classes of objects from old classes. I had trouble following some of these examples; is there a concept in C that is analogous to the concept of "classes" in Eiffel?

Nothing you've learned yet.


Miscellaneous

What is Eiffel and where is it used?

How often is Eiffel notation used and what is it used for?

Eiffel is a programming language designed for better programming-by-contract. It tends to be used more in Europe than in the U.S. It is designed for large, reliable, systems. I'm not sure how much it's really used for that purpose. It is also used as a teaching language, but by a relatively small set of colleges and universities (even fewer than use Scheme, I think).

Is your 6-p style of documentation particularly expired by this article?

I think you mean inspired. Both the article and the 6-P style tap the same well of common knowledge.

Does this necessarily disagree with the Unix design philosophy that commands (or functions, or programs) should silently accept as much input as possible, and fail spectacularly when it can't handle the input?

Yeah, it does seem to disagree with that philosophy.

I find it highly ironic that assignment 6, in which both the client and supplier check for errors, is due the same day as this reading.

It wasn't supposed to be ironic. The reading and the assignment were supposed to build upon each other.

I don't think I have any real questions about the content, but what was the reason behind the reading?

To help you think more about how you write robust code.

By comparing how programs and everything in it as a contract, integrity and reliability are two main concerns in this reading right? I mean sure, efficiency is imprtant, too. Right?

I think Meyer would suggest that it's pointless to have efficient code if you're not sure that it's correct.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Thu Mar 10 16:27:32 2011.
The source to the document was last modified on Thu Mar 10 16:27:29 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Readings/design-by-contract.html.

Samuel A. Rebelsky, rebelsky@grinnell.edu