Tracing Programs by Hand Tips & Tricks
When trying to work though a code tracing problem on a quiz, worksheet or any other assignment in your programming career, it is probably a good idea to pull out a pencil & paper. Keeping track of how different moving parts in your code are changing throughout the execution of a program is crucial and must be kept organized. Here are some tips and tricks!
- Make an environment diagram!! Environment diagrams are a great tool to systematically record the state of a program as it executes. You can keep track of how far you've gone by noting return values in frames from function calls that have completed & writing down line numbers to know where to return back to. Check out the pages on environment diagrams for more detail on the structure of these!
- Create a space in "memory" on your paper for each variable that is declared, and update its value on paper every time it is updated in code...if you are working in a single function, feel free to make yourself a chart to keep track of all of the changing variables!
- Keep track of variable scope. This is something that will be covered later in the semester but is a super important concept in programming. Variables can only be used in the scope that they are defined in and as a result, a program may have two variables with the same name & you will have to decide when to use or make changes to which. Again, environment diagrams are a wonderful tool to make name resolution a breeze.
- Keep printed output separate from the stack! Printed output displayed for the user is not the same as a variable or return value within a frame. It can be helpful to write down what gets printed as a program runs, but just keep it separate from the stack.