Debugging
Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior
Compile Problems Cannot find symbol:
Compile Problems Cannot find symbol: wrong parameters
Compile Problems Cannot find symbol:
Compile Problems Cannot find symbol: void constructor
Compile Problems Cannot find symbol:
Compile Problems Cannot find symbol: package not imported
Compile Problems Not abstract:
Compile Problems Not abstract: interface not implemented
Runtime Exceptions NullPointerException:
NullPointerException: variable not initialized
Runtime Exceptions in Library Code
Runtime problems: Incorrect behavior
Approaches to Debugging
bug - a defect (inappropriate behavior) in a piece of software debugging - the process of identifying the cause of a bug and repairing it it is more efficient to prevent bugs than to remove them
It is estimated that 75% of disease in the United States could be prevented by appropriate lifestyle changes (e.g, not smoking, exercising, eating appropriately, relieving stress) true or false? 75% of software defects could be prevented by appropriate software development methodologies: –analysis –design –code review –unit testing
But if you didn’t take sufficient preventive measures, or if you did but still have some bugs (the other 25%), you must debug the program how are bugs discovered? –through formal testing (good) –in use (bad) bugs can be extremely difficult to find, especially in large programs –sometimes a bug doesn’t manifest until millions of instructions later (after the code that caused the bug) –sometimes a bug manifests in a different part of the program than the part that caused it
Approaches to debugging: –debugging by flailing –debugging by experience –debugging by thinking
Heuristics (rules of thumb) for debugging*: –create a simple test case that always produces the bug (if its not repeatable, you can’t know if you’ve solved it) –describe the bug (in writing, and/or to someone else) Make a plan: –formulate some hypotheses –test the hypotheses –keep notes * adapted from Debugging by Thinking, by Charles Metzger
Techniques for debugging*: –read the source code! –describe the bug to someone else (very valuable) –print out the values of variables/objects –print out the names of methods that are executed –use more sophisticated instrumentation –use a debugger to follow the execution of the code, and to look at variables –use assertions to check assumptions * adapted from Debugging by Thinking, by Charles Metzger