Title: Debugging Why you were up till 2AM
1DebuggingWhy you were up till 2AM
2Debugging
- Definition The process of finding problems
(bugs) in programs and removing them
3Why do we call it debugging?
1947, one of the first computers the Harvard
University Mark II Aiken Relay Calculator On the
9th of September, 1947, when the machine was
experiencing problems, an investigation showed
that there was a moth trapped between the points
of Relay 70, in Panel F. The operators removed
the moth and affixed it to the log. The entry
reads "First actual case of bug being found." -
http//www.jamesshuggins.com
4General Steps
- Recognize that a bug exists
- Isolate the source of the bug
- Identify the cause of the bug
- Determine a fix for the bug
- Apply the fix and test it
5Isolating source of the bug
- Once found, and understood, most bugs are easy to
fix. We will concentrate only on finding the bug
6Confirm your beliefs
- Finding your bug is a process of confirming the
many things you believe are true, until you find
one which is not true - You believe that at a certain point in your
source file, a certain variable has a certain
value - You believe that in a given if-then-else
statement, the else part is the one that is
executed - You believe that when you call a certain
function, the function receives its parameters
correctly
7Confirm your beliefs
- So the processing of finding the location of a
bug consists of confirming all these things! - Check everything!
Okay How?
8One way
- Put lots of print statements in at each point
that says I am in method X with parameters set
to Y,Z - Note If you do this, be able to turn them on/off
easily! - Boolean DEBUG true
- If (DEBUG) System.out.println..
9Often a better way
- Use the debugger
- Set a breakpoint at a point in the code
- Step through the code
- Look at the variables (local and class variables)
to see ones that are not as you expected
The debugger is usually better/faster but print
statements are also easy to turn on/off as a
whole to make a DEBUG mode for your program.
Which is often useful.
10The Most Important Thing
- To do ANY debugging you must understand what the
program should do - What each method should do
- What each if statement should do
If your beliefs are wrong, confirming your
beliefs will not help!
11The Most Important Thing
- Add beliefs into your code beliefs are otherwise
known as COMMENTS!
12Lets work some examples
- See Netbeans debugging project
- Fibonacci - Compile time problems
- DebugMe - Use the debugger to find the problems
- WordAnalyzer -
- Test 1 - Use print statements
- Test 2 - Use print statements
- Test 3 - Use the debugger
13My Presidents arent sorted by party
- What are your beliefs? Or more specifically, what
are some hypothesis that you should confirm? - Youre not calling the sort routine at all
14My Presidents arent sorted by party
- What are your beliefs? Or more specifically, what
are some hypothesis that you should confirm? - Youre not calling the sort routine at all
- The sort is not seeing every president
- The sort is sorting by something other than party
- The array is not getting updated by the sort
- The array is getting sorted, then resorted by
something else
15My Presidents arent sorted by partyConfirming
your beliefs
- Youre not calling the sort routine at all
- Confirm put in a print statement inside the sort
routine saying DEBUG sorting begun - The sort is not seeing every president
- Confirm put in a print statement to print out
every president the sort loop sees - The sort is sorting by something other than party
- Write code that says sorted president (Reagan,
Rebulican) after (Clinton, Democrats)
16When I print I see duplicate Presidents
- During the sort I dont add any new data
- Confirm Check the length of the data
before/after the sort - During the print, I only print each value once
- Confirm compare the length of the list to the
number of times you issue a print statement - assert (length countPrints) Too many
lengthcountPrints
17Suduko cannot find a solution
- Confirm there is a solution
- Confirm youre reading the values in correctly
from the grid - Confirm youre able to access the correct value
using index -gt row, col conversion - Confirm your List can move both forward and
backward correctly - Confirm you are setting / clearing BitSets
appropriately (at various points print out the
BitSets for a row/col/box and the grid and compare
18Suduko cannot find a solution
- Confirm youre able to find the next value
correctly - Confirm youre updating the SudokuList
appropriately - .
19COMMENT YOUR CODE!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
COMMENT!
20Whoa there I cant even compile!
- First, try never to get here. Compile early,
compile often. If you cant compile at any point
STOP fix the code before adding any new code. - Review the compiler error, read it carefully
- Check your book and online resources for examples
and see what may be wrong - Ask questions on WebCT, to your TA or Professor.
21The Most Important Thing (again)
- Add beliefs into your code beliefs are otherwise
known as COMMENTS! - Or if you can java assertions!
22Java Assertions
- assert Expression1 Expression2
- If Expression 1 is not true, throw an error with
String in Expression2 - assert xlt4 Invalid x value x
- Assertions can be enabled during debugging and
disabled for production use (increasing
performance) - java -ea edu.gmu.MyClass // enable
- java -da edu.gmu.MyClass // disable
More info http//java.sun.com/j2se/1.4.2/docs/gui
de/lang/assert.html
23References
- http//heather.cs.ucdavis.edu/matloff/UnixAndC/CL
anguage/Debug.htmltth_sEc2 - http//en.wikipedia.org/wiki/Debugging