Table of ContentsUsing the Debugger > An Example of Using the Debugger for Good

An Example of Using the Debugger for Good

This section walks you through an example of using the Debugger to find a bug in the code and intends to show how really useful it is.

Gimme some code!

In order to use a debugger, the first thing you need is some code. So, here it is:

  # this function performs a swap of
  # two parameters
  def swap(a, b):
    holder = b
    a = b
    b = holder
    return a, b

This program is supposed to swap the input parameters, load it into your JES editor. So, suppose you do this in the command window:

  >>> a = 1
  >>> b = 2
  >>> a, b = swap(a, b)

Variable a should be left with the original value of b - 2, and b with 1 - that of a. But of course, there is a bug in it.
 

jump to top

We have a Bug!!

If you typed in the above code program and commands into JES you might see that the program is not behaving correctly. When you try to inspect the values of a and b:

  >>> a
  2
  >>> b
  2

That's not right! a is supposed to be 1 and b is supposed to be 2! NOOOOOO!

So, now what do you do? You have a program that runs but doesn't work like it is supposed to. There is a bug in the program.

jump to top
Doing Something About It

It would really help if you could see the result after each line of command executed, because then you can track the values and find out when the variables start having wrong values. One thing you could do is to use 'print' statements to print out the variables after each line of code. This is what a lot of programmer do, and is something that is valuable to learn. But since we have a debugger we can use and which does the same thing better, let's do it that way. Open up the debugger, and let's watch(monitor) all of the existing variables in the program - a, b, and holder. If you don't know how to do this, see section Using the Debugger.

Now run the above code in the command window again. And...tada!!!

Okay...let's see what's going on. Uh-huh...uh-huh...uh-Uh-Oh! On step 3 (line 5), all 3 variables contain the value 2. Where did the 1 go? We need at least one 1. Something must have went wrong at this line or before it. So, line 5 puts the value of b (2) into a, which had the value 1 previously. This effectively erases the value 1 that a held. That's where the 1 went. So, line 5 is the location of the bug.

Now we need to fix the bug. The problem is that we should overwrite b because its value has already been duplicated onto the holder variable, and so won't lose the value by writing to b, and that's the whole point of swap. So we change line 5 to:
  b = a

Now let's give it another try:

Typing the variable show:

  >>> a
  1
  >>> b
  2

Now the variables stayed the same. Dang it! But now look at step 3, b did get the value 1. At step 4 however, b took on the value 2 again. That's not good. If you look at the code (line 6): b = holder, the value came from holder. But what we want to do is give variable a the value1 ! So let's assign holder to a instead. Change line 6 to:
  a = holder

Now try again:

  >>> a
  2
  >>> b
  1

It works!! You always want to try it with different values just to make sure though:

  >>> a = 'Jenny'
  >>> b = 'Ben'
  >>> a, b = swap(a, b)
  >>> a
  'Ben'
  >>> b
  'Jenny'

It still works! Looks like our job here is done =) Good job. Young grasshopper.

jump to top