Few programmers get things right the first time. Even after creating
an extensive design, we often encounter problems when writing the code.
What do you do when your code contains bugs? How do you make sense of
the error messages that appear? This assignment is designed to help you
become self-sufficient at debugging. By practising debugging skills, you
will become more adept at spotting and fixing bugs.
Open up the Lightbulb Debugging Assignment. This
should enable you to turn each lightbulb on or off by clicking on its "on"
and "off" switches. Every time this is done, a message appears,
indicating how many times that particular lightbulb has been accessed. The
master switch at the bottom of the page turns all of the lightbulbs
simultaneously on or off.
Your task is to try out the buggy code, taking note of the error messages that
appear. Note that not all of the errors will be indicated at the very
beginning. This is because some of the bugs prevent some entire functions
from executing. Only when the general structure of the function is fixed
will more obscure bugs within the function be detected.
Debugging is not an easy task, so don't be frustrated if this assignment
takes you awhile to complete. Here are some hints to help you get started.
- Look at the line indicated by the error message. You'll want
to go to that line in the code to take a closer look at the error.
- Print out values. It is often helpful to print out the values
of certain variables to check if they are correct, and to tell you what
is happening in the program. It is possible the bug is caused by a variable
holding an incorrect value. Printing out a value within a loop, for example,
will show you how many times a loop executes, or if it executes at all.
- Adopt proper formatting techniques By spacing out your code
and indenting it properly, you will have an easier time reading it
and isolating errors.
- Fix each error and update your file.After you fix errors, save
your code, and reload the page. This will show you the results of your
debugging and perhaps print out more errors for problems that have
Finding common errors:
- Check code structure. Does the code contain all required
parentheses and curly braces? Are commenting tags closed? A missing
closing brace will prevent entire functions from working, so you may want to
check this at the beginning.
- Check for misspelled words. An error message stating that some
variable or function is not defined may actually be caused by a typo.
The program does not understand a misstyped word, and thus thinks that it
is not defined. Check for capitalizations of words as well.
- Look for incorrect values in loops and conditions. Are your loops
executing the right number of times and incrementing by the correct amount?
Are array positions correct?
- Check parameters and return values You may have neglected to
pass in a correct parameter to the function, or the wrong value is
being returned from a function.
These are just several things to check for when debugging. Of course,
debugging is hardly ever this predictible, so you probably encounter many
unique situations. As you debug, you will likely develop a plan of attack
with which you feel comfortable. Be patient, and good luck!