Dr. Mark Humphrys

School of Computing. Dublin City University.

Home      Blog      Teaching      Research      Contact

My big idea: Ancient Brain


CA216      CA249      CA318

CA400      CA651      CA668

How to debug a program

With the vast amount of sample code on the Internet, I often see students getting big chunks of sample code and trying vainly to get it to work. I also provide sample code which I ask students to modify. I have noticed students often do not have the right mindset when doing this.

Here are a few simple tricks for how to debug a program:

  1. Strip it down. Remove parts of it.
  2. Get smaller parts working first.
  3. Don't try to do it all at once.

  4. You don't have to delete code. Just comment it out. Then slowly comment it back in.
  5. Comment out lines of code:
    # code

    // code

  6. A trick is to use tabs to make it easy to comment code out and in:
    #	code
    #	code
    #	code
  7. Comment out blocks of code:
  8. Insert an exit after reaching a certain stage (comments out everything below it):
  9. Look at variables half-way through:
    echo $var



  10. Build all programs in stages, testing each stage.
  11. Slowly comment code back in.
  12. Slowly move exit further down or remove it.
  13. Slowly remove debug info.

Debugging is not a mystery. Yes, there are fancy debugging and tracing tools.
But half the time, a few well-chosen prints, exits and comment-outs are all you need to find the problem.

"Evolution of a Programmer".
From here.

Feeds      w2mind.org      ancientbrain.com

On the Internet since 1987.