Dr. Mark Humphrys

School of Computing. Dublin City University.

Home      Blog      Teaching      Research      Contact

My big idea: Ancient Brain

Search:

CA114      CA170

CA668      CA669      Projects


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
    #	code
    
  7. Comment out blocks of code:
    /*
    code
    code
    */
    
  8. Insert an exit after reaching a certain stage (comments out everything below it):
    code
    code
    
    	exit
    
    code
    code
    
  9. Look at variables half-way through:
    echo $var

    System.out.print(var);

    console.log(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.



ancientbrain.com      w2mind.org      humphrysfamilytree.com

On the Internet since 1987.

Wikipedia: Sometimes I link to Wikipedia. I have written something In defence of Wikipedia. It is often a useful starting point but you cannot trust it. Linking to it is like linking to a Google search. A starting point, not a destination. I automatically highlight in red all links to Wikipedia and Google search and other possibly-unreliable user-generated content.