21
Debugging

Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior

Embed Size (px)

Citation preview

Page 1: Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior

Debugging

Page 2: Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior

Debugging

• Compile problemsRead the whole complaint!

• Runtime problems– Exceptions– Incorrect behavior

Page 3: Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior

Compile Problems• Cannot find symbol:

Page 4: Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior

Compile Problems• Cannot find symbol: wrong parameters

Page 5: Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior

Compile Problems• Cannot find symbol:

Page 6: Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior

Compile Problems• Cannot find symbol: void constructor

Page 7: Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior

Compile Problems• Cannot find symbol:

Page 8: Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior

Compile Problems• Cannot find symbol: package not imported

Page 9: Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior

Compile Problems• Not abstract:

Page 10: Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior

Compile Problems• Not abstract: interface not implemented

Page 11: Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior

Runtime Exceptions• NullPointerException:

Page 12: Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior

NullPointerException: variable not initialized

Page 13: Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior

Runtime Exceptions in Library Code

Page 14: Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior

Runtime problems: Incorrect behavior

Page 15: Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior

Approaches to Debugging

Page 16: Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior

• 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

Page 17: Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior

• 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

Page 18: Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior

• 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

Page 19: Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior

• Approaches to debugging:– debugging by flailing– debugging by experience– debugging by thinking

Page 20: Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior

• 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

Page 21: Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior

• 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