First the fives, then the sixes: an exam on proving programs correct
At university we had a subject along the lines of the theory of programming, where you had to prove that a program works correctly. The approach really struck me at the time: it was something new. Not testing, not eyeballing, but actually proving it mathematically.
The idea goes like this. There is an invariant, a condition that must hold all the time. Before the loop you check that it’s true. At each step you check that the step doesn’t break it. And on leaving the loop, the invariant together with the exit condition gives exactly what you need. For example, for the sum of an array the invariant is “s equals the sum of the first i elements”: before the loop it holds for i = 0, every step keeps it true, and when i reaches n, s is the sum of the whole array.
Everyone came to the exam. The teacher asks:
“So, is all the homework handed in?”
“Yes, it is.”
“Good. Whoever wants a five automatically, come up and I’ll put it down.”
Half the people rushed off. But my friend sits quietly: let’s sit, let’s sit, it’s too early. When they had left, the teacher says:
“Right, guys, and now whoever wants a six, come up and get it.”
Then my friend went too, and literally a handful of people were left in the room: those who wanted to sit the exam.
I did it honestly: drew a question and started answering. Apparently, until then I hadn’t quite understood something, but right there in the exam it clicked and I started explaining. Only I had already slipped up at the very start, and in the end I got a seven. Was it worth the trouble for two extra points? For me, it was. The mark was a bit lower than I’d expected, but fine on the whole. And the main thing was that I finally understood how it works.
Just like in the joke: first they hand out fives, then sixes.