Rule 4: Storage Controls Tests
You need some way to keep track of changing data while the code operates, and this is done with “storage.” Usually this storage is in the computer’s memory and you create names for the data you’re storing in memory. You’ve been doing this when you write code like this:
1 x = 10 2 y = 20 3 z = x + y
In each of the previous lines we’re making a new piece of data and storing it in memory. We’re also giving these pieces of memory the names x, y, and z. We can then use these names to “recall” those values from memory, which is what we do in z = x + y. We’re just recalling the value of x and y from memory to add them together.
That’s the majority of the story, but the important part of this little rule is that you almost always use memory to control tests.
Sure, you can write code like this:
1 if 1 < 2: 2 print("but...why?")
That’s pointless though since it’s just running the second line after a pointless test. 1 is always less than 2, so it’s useless.
Where tests like COMPARE_OP shine is when you use variables to make the tests dynamic based on calculations. That’s why I consider this a “rule of The Game of Code” because code without variables isn’t really playing the game.
Take the time to go back through the previous examples and identify the places where LOAD instructions are used to load values, and STORE instructions are used to store values into memory.