SEARCH W7R

Tuesday, November 22, 2011

How to Debug a Computer Program

There are a ton of things that could and will go wrong when coding a program:

Systematic Error; Syntax Error; Mathematical (Calculation) Error; Infinite Loop; Null Pointer Exception; A Computer Hardware Error; Invalid Input; Transition Error; Compilation Error........



Nearly all programming errors can be fixed using print line statements:

Insert 3 (or so) print lines in the most important parts of your programming. This would probably be best done with no print lines in the same general area of the others. Keep in mind that what the line prints out to the console, or whatever interface, will need to be identifiable.

Example 1: (java print lines for debugging) [CODE]
System.out.println("This may be error. LOCATION ONE. Method:__________");
System.out.println("This may be error. LOCATION TWO. Method:__________");
System.out.println("This may be error. LOCATION THREE.  Method:__________");

 History tends to repeat itself so try to think back to your previous coding errors and think of where those may occur in your current program.
Inserting print lines will make your program slower and bigger, but it useful for isolating problematic areas.

If I put a print line statement in every loop that occurs in my program that all have a different output, I could run the program and look for which output keeps repeating itself.

[YOUR CONSOLE/OUTPUT]
Loc 1
Loc 1
Loc 1
Loc 1
....

if my program keeps outputting Loc 1 then I have most likely found the faulty loop.

Infinite loops are one of my errors you could have, so you are probably wandering what about the other errors. The print lines can take care of those various errors by outputting class variables to the console. For example, a mathematical or calculation error could be identified if a method or function called getEvenInteger() outputs an odd number to the console like so:

[YOUR CONSOLE/OUTPUT]
2
4
8
6
10
9
2

The "9" that was printed out should not have been printed out by the getEvenInteger method (or function) because 9 is not an even number.
Knowing this, you should attempt to solve your error from inside your getEvenInteger method.

This process if better defined as debugging by isolation as opposed to print line injection debugging.

No comments: