



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




If you are spending a lot of time debugging, it is probably because you do not have an effective program development plan. A typical, bad program development plan goes something like this:
The problem, of course, is the first two steps. If you write more than one method, or even an entire method, before you start the debugging process, you are likely to write more code than you can debug. If you find yourself in this situation, the only solution is to remove code until you have a working program again, and then gradually build the program back up. Beginning programmers are often unwilling to do this, because their carefully crafted code is precious to them. To debug effectively, you have to be ruthless! Here is a better program development plan:
After every change, the program should produce some visible effect that demonstrates the new code. This approach to programming can save a lot of time. Because you only add a few lines of code at a time, it is easy to find syntax errors. Also, because each version of the program produces a visible result, you are constantly testing your mental model of how the program works. If your mental model is erroneous, you will be confronted with the conflict (and have a chance to correct it) before you have written a lot of erroneous code. One problem with this approach is that it is often difficult to figure out a path from the starting place to a complete and correct program. I will demonstrate by developing a method called isIn that takes a String and a Vector, and that returns a boolean: true if the String appears in the list and false otherwise.
public static boolean isIn (String word, Vector v) { System.out.println ("isIn"); return false; }
Of course, to test the method we have to invoke it. In main, or somewhere else in a working program, we need to create a simple test case. We’ll start with a case where the String appears in the vector (so we expect the result to be true).
public static void main (String[] args) { Vector v = new Vector (); v.add ("banana"); boolean test = isIn ("banana", v); System.out.println (test); }
If everything goes according to plan, this code will compile, run, and print the word isIn and the value false. Of course, the answer isn’t correct, but at this point we know that the method is getting invoked and returning a value. In my programming career, I have wasted way too much time debugging a method, only to discover that it was never getting invoked. If I had used this development plan, it never would have happened.
public static boolean isIn (String word, Vector v) { System.out.println ("isIn looking for " + word); System.out.println ("in the vector " + v);
for (int i=0; i<v.size(); i++) { System.out.println (v.get(i)); String s = (String) v.get(i); if (word.equals (s)) { System.out.println ("found it"); } } return false; }
As always, we use the equals method to compare Strings, not the == operator! Again, I added a print statement so that when the new code executes it produces a visible effect.
public static boolean isIn (String word, Vector v) { System.out.println ("isIn looking for " + word); System.out.println ("in the vector " + v);
for (int i=0; i<v.size(); i++) { System.out.println (v.get(i)); String s = (String) v.get(i); if (word.equals (s)) { System.out.println ("found it"); return true; } } return false; }
If we find the target word, we return true. If we get all the way through the loop without finding it, then the correct return value is false.
If we run the program at this point, we should get
isIn looking for banana in the vector [apple, banana, grapefruit] apple banana found it true
public static boolean isIn (String word, Vector v) { for (int i=0; i<v.size(); i++) { System.out.println (v.get(i)); String s = (String) v.get(i); if (word.equals (s)) { return true; } } return false; }
Commenting out the print statements is a good idea if you think you might have to revisit this method later. But if this is the final version of the method, and you are convinced that it is correct, you should remove them. Removing the comments allows you to see the code most clearly, which can help you spot any remaining problems. If there is anything about the code that is not obvious, you should add comments to explain it. Resist the temptation to translate the code line by line. For example, no one needs this:
// if word equals s, return true if (word.equals (s)) { return true; }
This code gets an object from the vector and checks whether it is a String. If it is, it performs the typecast and assigns the String to s.
As an exercise, modify isIn so that if it finds an element in the vector that is not a String, it skips to the next element.
If we handle all the problem conditions, we can prove that this method will not cause a run time error.
We haven’t proven yet that the method is semantically correct, but by proceeding incrementally, we have avoided many possible errors. For ex- ample, we already know that the method is getting parameters correctly and that the loop traverses the entire vector. We also know that it is comparing Strings successfully, and returning true if it finds the target word. Finally, we know that if the loop exists, the target word cannot be in the vector.
Short of a formal proof, that is probably the best we can do.