# Bug Free Programming

Research has shown that there is a factor of 20 between the productivity of the best and the worst programmers. That is, top programmers work 20 times faster than the worst ones, and around 5 to 10 times faster than average programmers. This means that a very good developer can do in a day what others may need a month for. There are a couple of ways to increase productivity, but the most important factor is accuracy.
Accuracy strongly correlates with development speed. The best developers distinguish themselves by writing bug free code right away. If you are able to write code that does not contain any mistakes, you don't have to waste your time hunting bugs. Ever wondered why that guy at the next table who works so leisurely gets things done a lot faster than you? That's because he does the right thing at the first try. It is of course impossible to prevent bugs entirely, but:
Working fast means working accurately rather than working hard.
So how to write bug free code? You'd think that most bugs are introduced by thoughtlessness, but in fact, most bugs are introduced by the programmer not entirely understanding his own code. If you get a NullPointerException in Java, that's usually not because you forgot to think about null references, but because you didn't really understand the role of the variable that was null.
The trick to bug free programming is a rock solid understanding of the code you write.
To write bug free code, you first have to learn how to prove that a piece of code works. A lot of developers study these techniques at the university, but they never really learn to apply them in real-life coding. Other developers (especially those without a degree) never learn formal methods in the first place, so it's a good point to start. It may look too theoretical at first sight, but this is the key to bug free programming.
Let's start out with a very simple program. The goal here is that variable x of type int contain the value 10 when the program terminates. We will use C++ for demonstration throughout this book.
This code is obviously bug free. To prove it, let's add conditions enclosed by braces before and after each line of code. Conditions are logical statements that are true at the point of the condition in execution time no matter how execution arrives there.
The first two conditions are true, because we don't have any information at those points (note that C++ doesn't preinitialize variables). The only thing we know is true there is true itself. However, after the line x = 10 is executed, we know that the value of x is 10, because this is the definition of assignment. We can state this in more general terms:
It means that whatever was true before, after an assignment of a to x, x==a will hold true, i. e. the value of x will be a. We will treat it as an axiom of constant assignment.
The following program also assigns the value 10 to x, but it does it differently.
To prove that it's correct, annotate it with conditions again:
After the assignment to y, we can state that its value is 10. Well, if y is 10, and we assign it to x, x will also be 10. This leads to the full axiom of assignment:
Similar rules apply to when there is an expression at the right hand side of the assignment. Another important axiom is that assignment doesn't change any variables that are not assigned to.
This is still very trivial and is only needed to build the rest around it.
You've probably realized by now that there are two conditions to consider about each assignment: one that holds true before it, and one that holds true after it. The former is called precondition, the latter is called postcondition. We will prove the correctness of a program by chaining them: the postcondition of a statement is the precondition of the next statement.
Here S1; S2 means two statements executed after each other, i. e. in a sequence. There are two other types of control structures: conditionals and loops. We'll learn to reason about them later.
One final axiom we need to start proper correctness proving is the notion that:
A => B means A implies B in the logical sense.
If this is all Greek to you, don't panic. We'll cover enough examples for these thigs to start making sense.
Now let's do our first formal correctness proof. Prove that the following program assigns 10 to x.
Annotate it:
We have the annotations, but we have to prove that they indeed hold true at the point where they are written.
This is trivial because variable definitions do nothing from a correctness point of view. Now we have to prove that:
This is because of the axiom of assignment. Since it is an axiom, we don't have to further prove it. We can now use this as the precondition of the next statement:
and again:
Why does this prove the original statement? If we name the statements and the conditions like this:
and use the axiom of sequence three times, we get:
We essentially proved that the program as a whole goes from {true} (whatever) to {x == 10} (x having a value of 10).
This is what we had to prove to make 100%, absolutely sure that the code is bug free. Note that now there is no doubt that this code is correct (unless we did a mistake in the proof, of course). What made this possible is the fact that we had a very clear understanding about what was going on in the code at every possible point of execution. Annotating the code with conditions made our expectations explicit, and having to prove them in a formal way made us think about why these expectations were true.
Nobody has the time to do formal correctness proofs in real life situations, but knowing how to do it helps a lot in creating bug free code. The trick is to be explicit about expectations. This prevents you from forgetting about null references, among other things.
Let's move on to conditionals.
We want to prove here that y is even at the end. The axiom of conditional is as follows:
To prove that a conditional is correct, you have to prove two things. First, you have to prove that when the condition is true, the "then" clause gives correct results. Second, you have to prove that when the condition is false, the "else" clause gives correct results. Annotating the former example:
Check. We didn't explicitly assign a value to x and yet we will prove the correctness of a program that uses its value. C++ doesn't preinitialize local variables. The value of the variable is some memory garbage. We have a conditional here, so we have to check for the then and the else clause separately.
Obviously
Now if you use the axiom of relaxation:
Now let's go for the else clasue
Obviously
Again, using the axiom of relaxation:
Now combine the results with the axiom of conditionals:
I know you don't believe that it's of actual real life use, but keep reading. We will eventually drop the maths and only use the core notions of the proofs to produce bug free code.
Let's write our first program with some actual value. Say we have an array of int 's, and we want to put the sum of its elements into a variable called sum. We will learn how to reason about loops in this example, and this will be the first time you'll see how it helps you write correct code right away. We will start with a pre made program, but later we will investigate how to turn the techniques used in correctness proofs to design the code.
Precondition Q is stated to makes sure we don't try to read unallocated memory. We don't care how the array is allocated or filled with numbers for this example.
Loops are the most complicated control structures of the three, and the most prone to mistakes. The difficulty is that a loop is a series of conditionals in disguise, and you don't even know how many conditionals it is, because it depends on the condition which is evaluated at runtime. If you try to reason about loops by visualizing how they execute, you are in trouble, particularly if you have more complicated loops than the previous one. Visualization is an important step in getting a rough idea about how to code something, but if you don't want to spend your evening before the deadline debugging corner cases you didn't visualize, you'd better reason about your code statically. That does not necessarily mean mathematically. We will eventually drop the math completely.
The trick is to convert the dynamic nature of loops into a static condition. If we could prove a condition that is true every time before the condition is evaluated, we could reason about the loop without relying on visualization. This condition is called the invariant of the loop.
We'll only care about the loop in this example because the rest is trivial. The invariant is that sum contains the sum of the elements from index 1 to index i-1 every time before the condition is evaluated, including when execution first arrives at the loop.
When execution hits the loop:
holds. Does it imply I? Of course:
This is true, because there are no elements between index 0 and index -1, and the sum of 0 numbers is 0. That is, the sum of all the elements of an empty array is 0. If the array is not empty, the body of the loop will execute, and the condition will be evaluated again. At this point I is true (we don't consider loop conditions with side effects, more on that later). The condition of the loop is also true, because otherwise, we wouldn't have entered the loop body. What we have to prove is:
That is, the invariant is preserved across iterations. What's the condition to use between the statements? sum has been updated, but i hasn't. Try:
Can we prove that
? We have a slight problem here, because we can't prove that array[i] is a valid expression. i must be smaller than length and at least 0 for it to be valid. Additionally, we need array to be an allocated array with at least length elements (let's denote this condition with T to save space). Therefore, we have to modify our invariant to include this information.
Note this important step in understanding the code we write. We didn't make all our expectations explicit. This is how bugs sneak in. With this new invariant we have to start over. Is it true when execution hits the loop?
This is true. For example, we know that i <= length because i == 0 and length >= 0. Is it preserved across iterations?
Find the right condition after updating sum:
We know that array[i] is valid, because of the constraints on i included in the condition. What about
We changed i to i-1 because it's the original value of i (before the statement was executed).
because
For example 0 <= i-1 implies 1 <= i which implies 0 <= i. i-1 <= length and i-1 != length together imply that i-1 < length which in turn implies that i <= length.
When the loop terminates, its condition is false. Additionally, the invariant is true. This is because it's true every time before the condition is evaluated. If we can prove that
we prove that R is true when the loop terminates. It's trivial:
We have proven that the program gives correct results if it terminates. However, note that we haven't proven that it actually terminates. This is called partial correctness. Full correctness means to also prove that the program terminates. We will come back to it later.
Make sure you understand the above. I only gave a couple of examples, because this is not a book on formal correctness proving. If you feel you need to dive deeper into it, look up some book on Hoare's method. The rest of the book will not make much sense if you don't understand it.
So how does this help you work faster? You don't have the time to work out the formal proof of your code all the time, but knowing how to do it will help you even when you aren't using it. A good understanding of preconditions, postconditions, and invariants will help you write much better code. Now we will cover plenty of examples that show how.