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:
- Strip it down. Remove parts of it.
- Get smaller parts working first.
- Don't try to do it all at once.
- You don't have to delete code. Just comment it out. Then slowly comment it back in.
- Comment out lines of code:
- A trick is to use tabs to make it easy to comment code out and in:
- Comment out blocks of code:
- Insert an exit after reaching a certain stage
(comments out everything below it):
- Look at variables half-way through:
- Build all programs in stages, testing each stage.
- Slowly comment code back in.
- Slowly move exit further down
or remove it.
- Slowly remove debug info.
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".