Debugging
Today's lecture is to help students understand how to correct their programs with they have errors.
What is Debugging? Program compilers are fussy--they expect programmers to write programs with correct syntax (grammar) all the time. Moreover, programs they do exactly what you specify, so if your program provides the wrong instructions, it will do the wrong thing. Because programming is a complex task, we very often don't get it right the very first time. So to determine if we've written the program correctly, we test our programs and correct any errors. The term we use to find and correct errors is called debugging.
Error Types: There are three types of errors in programming, you can read about them in section 1.4 of our text (pgs 30-31).
Debugging Techniques: The key to getting rid of errors in your program is to be systematic: that is, you should have some kind of plan to address each of the errors one-by-one until they have been eliminated. Below are some suggestions for removing errors of different types.
Syntax
These are the easiest errors to address, because the compiler let's you know about them and usually tells you were they are within a line or two. When fixing syntax errors follow these guidelines:
- Fix only the first one, then recompile. If you lucky the rest may disappear.
- Look for the error first on the line indicated by the compiler. If you can't find it there, look at the line just above this. NOTE: the error will NEVER be after the line indicated.
- Try to remember what kinds of common errors there are which you may have seen before, e.g., forgot a semicolon, forgot a parentheses or brace, misspelled a variable or did not declare a variable, etc
Run-Time
These errors are also easy to find, once you know they are there, because the cause your program to terminate abnormally. The issue is getting them to occur, because they may not occur for all executions of your program (i.e., they may be dependent on the input data). The key then is to use a wide array of test conditions to fully exercise you program.
Logic
These are the toughest errors to uncover, because there may be no apparent indication of an error. Again, good test cases with known results are the best means for uncovering this type of error.
Demonstration of Common Compiler Errors and Warnings: Use programs from last lecture to demonstrate the kinds of errors that can occur when developing programs. Helpful examples to demonstrate are: