Editing and Rerunning
Programming becomes easier when changes are controlled. Do not change five things at once and hope the result makes sense. Change one thing, rerun, and compare.
A Small Loop
Start with:
Output:
double: 8
Now change only value = 4 to value = 7 and rerun:
Output:
double: 14
Because you changed one line, the new output is easy to explain.
Change one value
Run the code, change value, then run it again.
Ready to run.
Make the Program Tell You More
When you are unsure, print an intermediate value:
This is simple, but powerful. Many debugging sessions begin by printing the value you think you understand.
Avoid Random Editing
Random editing looks like this:
- change a number
- rename a variable
- remove a line
- add a package
- rerun and hope
That is hard to learn from. If the output changes, you do not know which edit caused it.
Controlled editing looks like this:
- state what you expect
- change one thing
- rerun
- compare output with expectation
This habit carries directly into NumPy and Deep Learning. Later, you may change a batch size, a learning rate, or one model layer. The same rule applies.
Edit only the value so the output contains double: 18.
Change one thing
Ready to run.
HintWork backward from the output
The program multiplies value by 2. Which value makes that product 18?
SolutionUse nine
Set value = 9. Then value * 2 is 18, and no other line needs to change.
This is a controlled edit because the changed output has one clear cause.
The Habit to Keep
A useful programming habit is not avoiding mistakes. It is making a small change, observing the result, and using that evidence.