The Persuasive Programming StyleBy Jerud J. Mead and Anil M. ShendeJerud Mead, professor of Computer Science at Bucknell University, and Anil Shende, professor of Mathematics, Computer Science, and Physics at Roanoke College, are co-authors of the forthcoming textbook Guide to Persuasive Programming. Watch for its release in late spring 2001.For years students in introductory programming classes have been fed a diet of formal syntax and very informal semantics. They have been encouraged to write nicely formatted programs with imprecise comments documenting more the structure of the code than its meaning. Check out the following vignette: VignetteProgramming by Approximation
And on he goes into the night. This is a programming style, familiar to most programming instructors, that we call programming by approximation. In this style the student repeatedly manipulates a program based on very fuzzy semantic understanding in the hope that the next small modification will result in a program that better approximates its specification (the correctness condition). Sam seems to understand that there is a relationship between changing the loop bound and the programs output, but he doesnt analyze exactly what the effect of the change will be. Through this repeated process of survival-of-the-fittest modification, Sams program evolves. In the end, Sam has a program whose execution approximates (or maybe meets) the specification, but he may have no clear idea why the program behaves as it does. Okay, this picture of student programming is not entirely fair. Most students have a good understanding of the semantics of the assignment and IO statements and even of simple selection statements and abstraction calls. But when it comes to more complex abstraction calls and selection and especially repetition statements, most students' semantic intuition is fuzzy at best. This intuition is even fuzzier when these basic statement types are combined through nesting or sequencing. Contrast with Sams plight the situations of two other students: Sue, who is working on a programming assignment for her CS1 class, and Tom, who is working on an essay for a history class. VignetteWriting vs. Programming Tom is equally certain as to what is expected of him. He knows that a thesis and a simple listing of historical facts will not do. He knows that the facts must be accompanied by supporting evidence set in a logical framework so that the reader of the essay (i.e., the instructor) is persuaded that the thesis is valid. This is a rhetorical (we will use the term persuasive) style of writing. The contrast is clear. Sue expects to produce a program that produces a specified output (analogous to the facts and thesis of Toms essay). But there is no perceived requirement that the program be accompanied by a convincing explanation for the instructor that the program does what it's supposed to dojust the evidence from test executions of the program. Lets check up on Sam again to see if he has made any progress. Perhaps we can gain some new insight on the programming process. VignetteWhy Comment? Finally done!! Now all I have to do is comment the code. Lets see, I can comment each variable declaration. Thats easy. Then Im supposed to put in each function . . . what are they called? Oh right, pre- and post-conditions. Then some comments in the code and I'll be done. Here is a sample from the code that Sam turned ina function insert that is meant to insert a value into an ordered list. //******************************************************
//********************* insert *************************
//******************************************************
void insert(List list, int value) {
// pre: list is a list and value is an integer
// post: value is inserted in list
...
// this loop inserts value in list
while(...) {
...
X = X + 1; // increment X by 1
...
}
}
So what kind of comments do we have here? The first three lines may be useful for signaling the beginning of the code for this particular function, but, in fact, they are purely syntactic. The pre- and post-conditions are welcome, but they dont really convey information not already implied by the function's name. Where is the value inserted? Is the list sorted before insert is called? After? And the comment before the repetition statement seems to be more a statement of hope than of fact. The comment on the assignment statement is welcome, because it is an attempt to represent the semantics of the commented statement. But the semantics of this particular assignment statement is so simple that the comment is redundant. In our experience such comments can be found in the programs of even experienced programmers. The meaning in such comments is often inversely proportional to the complexity of the statement being commented. Comments appear before a statement, not after; the pre- and post-conditions are associated with a function's definition, but are not restated at the point of a call to the function, where the program state is actually affected. Persuasive Programming To address these problems, a new, semantics-based approach to programming and programming education is neededan approach that redirects the attention of the student programmer to semantics; an approach that integrates semantics from the start and provides a mechanism for representing semantic content within the text of a program. This redirection is the goal of our new approach to programming, which we call persuasive programming. The persuasive style makes use of assertions (in the form of program comments) to reflect properties of a programs state (i.e., semantic content) at selected locations in a programs text. Including the assertions naturally draws the programmers attention toward the semantic side. A program written in the persuasive style will carry evidence of its own correctness. In this way, persuasive programming could also be called a rhetorical style for programmers. Teaching Persuasive Programming
// Assert: X == 5 AND Y == 3 X = X + Y * 3; // Assert: X == 14 AND Y == 3
// Assert: X > 0 X = X - 1; // Assert: X >= 0
// Assert: cin == <v, w, r, ...> cin >> X >> Y; // Assert: X == v AND Y == w AND cin == <r, ...>
// Assert: X > 0 // Assert: X > 0
Count = 0; Count = 0;
while (Count < X) { while (Count < X) {
// Assert: Count < X
Count = Count + 1; Count = Count + 1;
// Assert: Count <= X // Assert: Count <= X
} }
// Assert: Count >= X AND
// Count <= X
A final point is necessary. While it may seem that this persuasive style might require lots of extra class time, this has not been our experience. What we have found is that once the notions of state and assertion are discussed, then the presentation of statement semantics can be accomplished by using assertions and state to clarify concepts that are already being explained. Assertions and state just provide a context for presenting normal CS1 material. What this says is that persuasive programming is a process that must be experiencedit shouldnt be taught. Students should see the persuasive style in examples, lab assignments, and project solutions. By always seeing the asserted style, they will come to accept it as the accepted way to program. |