



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!




I see many of you wasting large blocks of time on debugging. As you already know, the requirements of this class are such that you cannot afford to waste any time. Below are my suggestions for debugging. I have been programming for 35 years! While I don’t pretend to have all the answers, these techniques work! Give them a try.
public class BadData extends Exception { public BadData(String msg) { super(msg); } } public void assert(boolean b,String msg) { try{ if (!b) { throw new BadData(msg+"Assert Violated"); } } catch (Exception e) {e.printStackTrace(); System.exit(1); } }
Using assert statements, place a variety of tests in your code. For example, verify input files were opened correctly, verify input data is correct, verify array bounds are within range. Simple assert statements take but a few minutes to insert, yet can save you hours. When I include asserts, I often think I am doing someone else a favor, but they end up helping me.
character biographies, objectives and units of actions. You would need to look at the play in more advanced ways than an individual word analysis.
I put such debug statements in my code preceded by the conditional ``if (debug)'' Then I set debug to true or false depending on what I want to see printed. I leave these debug statements in the final version. When you ask for help, these printed statements are much better than a verbal description of what you saw in the debugging window.
Many of you are too impatient to spend the time approaching the problem of debugging in a methodical manner. Some of you are ``too busy sawing to sharpen the saw.'' Take some time to insert strategic print statements. This practice will save you hours of debugging time.
print prettily'' (a routine that prints trees like we draw them) andprint array''. These routines should be called whenever you have a question about what the data structure really contains. Don't debug in the dark. Sometimes people say to me, ``I know this works. I've done it on paper five times, but when I run it I don't get the right answer.'' Just get debug information so you verify that what you think is happening is happening.Lots of times when I suggest putting in extra debug statements, I meet with hostility, Well, I can see what the statement does. Printing out key values won't do any good. That won't solve the problem.'' The simple fact is this: if the code were doing what you “ _know”_ it is doing, it would be working. Since it isn't working, you need to discover the difference in what should be happening and what is happening. If you are getting integer division instead of real division or if you are doing assignment (a=1) instead of comparison (a==1) or if you have inadvertently put a semicolon after your while condition, you can read the code dozens of times without picking up on the error. If you insertalmost ridiculous'' debug statements, the errors will quickly become apparent.
This isn't working correctly. See, the output isn't right. I've tried several things, but the output still isn't right. Tell me what is wrong.'' When I ask them,Is the data read in properly? What are the values of the variables at this point? What is stored in the array? Does this statement ever get executed?'' the answer is always, ``I don't know.'' There is a fundamental law of debugging: You cannot fix the code by merely observing the output. You must create information which tells you what is going on inside the code.What you are doing is analogous to the following scenario: You are a mechanic. Your car won't start. You say to yourself. Gee the car won't start. I'll try giving it more gas. Nope, still doesn't start. I'll try putting in oil. Nope, still doesn't start. I'll try a new
Come for help having tried all the basic things. For example:
a) If you have tried everything, you may try to simplify the program or input data. Get rid of as much input as you can and still produce the same problem. Make a test copy of your program and then get rid of as many lines as possible and still produce the error. This makes it much simpler for you and others to look at. For example, if the input routine isn't working properly, make a copy of the program which contains only the input routine. Get it working before combining it with other parts of the program. Some people always code this way - getting each routine working properly before going on to the next one. If you have a great deal of trouble debugging, this technique is strongly recommended. b) Narrow down the problem. Use print statements to verify that each module works correctly. For example, if your program consists of three major phases, make sure each one has produced the correct output. Then as you debug, successively add MORE and MORE debug statement in areas where you suspect the problem lies. Let your code talk to you. c) If a module appears to be doing something totally impossible, use detailed print statements to verify exactly what is happening. If a value isn't being passed back correctly, print it before it is returned to make sure it is computed correctly. If an if statement isn't giving the correct results, print important values to figure out why. If a certain branch doesn't appear to be executed, use a print statement before and after that section of code to print the conditional value and verify the code was reached. d) Don't jump to the conclusion that the compiler has a fundamental error, e.g. it can't compile the if statement. Although compiler errors do occur infrequently, the chances of you finding a compiler errors are extremely small.
In fact, don't jump to any conclusion. Verify any hunch you have.
These steps are critical in debugging efficiently. Many times I have students come to my office, very frustrated with a problem. Often, they have neglected most of the above steps as they have found a better way'' (which doesn't work). Since your programming assignments over the course of the next few semesters get very complicated, you can NO LONGER afford to debug casually. You must become experts at it. It is really a fun part of the programming process if you do it correctly. It requires sophisticated logic -What error would give that kind of result?'' As the semester progresses, I will become increasing persistent about your debugging skills. Give it your best effort before coming for help.
That said, the single biggest help to debugging it to program so it works the first time. This seems like obvious advice, but much is being said.
You wouldn’t paint a mural by first deciding the color and placement of the left hand of the townsperson. You would plan the overall composition of the painting and then add more and more detail, in layers.