Dr. Mark Humphrys

School of Computing. Dublin City University.

Online coding site: Ancient Brain

coders   JavaScript worlds

Search:

Free AI exercises


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.






How to debug, by the Fireship channel.



ancientbrain.com      w2mind.org      humphrysfamilytree.com

On the Internet since 1987.      New 250 G VPS server.

Note: Links on this site to user-generated content like Wikipedia are highlighted in red as possibly unreliable. My view is that such links are highly useful but flawed.