Docsity

Debugging: A Comprehensive Guide to Identifying and Fixing Errors in Java Code, Exams of Computer Science

Partial preview of the text

Download Debugging: A Comprehensive Guide to Identifying and Fixing Errors in Java Code and more Exams Computer Science in PDF only on Docsity!

1

Debugging

2

Debugging

  • Despite the best efforts at design, development, and testing, your code will still likely contain errors.
  • Programming is one of the most difficult things people do!
  • When you have errors, how do you find them? - Use System.out.println (slow, painful way) - Use a debugger (much better way)

3

How to Debug

  • Right-click on the java file with the main function
  • Select Debug as…→ Java Application
  • You may be asked to switch to the Debug Perspective. Just click on yes.

4

Breakpoints

  • If you follow the steps from the previous slide, you probably will just run through the entire program and not stop
  • You need to put a breakpoint in your code
  • Breakpoint: A place in your code where you would like to pause while debugging

7

Inspecting variables

  • In debug mode, you will see the following window at the top right

8

Inspecting Variables

  • In the Variables tab, you can inspect the values of variables
  • In the Breakpoints tab, you can see all the breakpoints you have active/inactive
  • In the Expressions tab, you can evaluate the results of certain expressions

9

Inspecting Variables

  • If you right click on the Expressions window, you can add any expression you would like to evaluate as the code is running
  • An expression can be a variable, a bunch of operations on variables, a function call, etc.
  • This is a very useful tool!

10

What if those tabs aren’t

there?

  • Go to Window → Show View (you may have to then select Other)
  • Under Debug, select Variables / Breakpoints / Expressions

13

Finally

  • Remember to terminate (hit the red square)
  • And clean up your threads (hit the two black Xs)

14

Steps in Debugging

  • Reproduce the error. Make sure you can show it repeatedly.
  • Simplify the error. Find the simplest test case or set of values that show the error.
  • Divide and conquer. Use breakpoints, print statements, to locate where the error occurs
  • Know what your program should do.

15

Steps in Debugging, cont

  • Keep an open mind. "When you have eliminated the impossible, whatever remains, however improbable, must be true"
  • Look around. If you don't see anything at the point the error is manifest, the mistake is probably earlier.
  • Look at the details. The simple errors are the hard ones to see.