1.9. 👩💻 To Understand a Program, Change It!¶
To check your understanding or your predictions, you will run a program.
To check your understanding about the state of variables before your code snippet runs, add diagnostic print statements that print out the types and values of variables. Add these print statements just before the code snippet you are trying to understand.
If you made a prediction about the output that will be generated when the code snippet runs, then you can just run the program. If, however, you made a prediction about a change that occurs in the value of a variable, you will need to add an extra diagnostic print statement right after the line of code that you think should be changing that variable.
The diagnostic print statements are temporary. Once you have verified that a program is doing what you think it’s doing, you will remove these extra print statements.
Even if you feel that you have a good grasp on the program though, we advise changing it at least a few times to see if you understand how it behaves in different situations. Sometimes you’ll surprise yourself!
If you get any surprises, then you will want to revise your understanding or your predictions. If you were wrong about the values or types of variables before the code snippet was run, you may want to revisit your understanding of the previous code. Once you understand how that result came to be, you should make some changes to the program to make sure your new understanding is accurate.