Day 7: The Importance of Writing Pseudocode
Programming involves solving a lot of abstract problems. It’s always a good idea to tackle this problems by writing a plan first. Without a plan, we might as well just be randomly hitting our keyboards and hoping for the best.
This is what comments and “pseudocode” is for. Pseudocode is basically just typing out what we want to do in English (or your native language). This gives us a written idea of what our actual code should be doing, so that we don’t get lost.
Let’s continue from where we were last time and look at an example.
Here, we want to restrict the bounds of the player’s movement so they can’t go too far up or down. I’ve written a plan of how to do this with comments, and then we can go actually write it with C# code.
What’s nice is that any time our pseudocode has the word “if”, that’s a cue to write an if statement in the actual code.
It’s even more important to do for more complicated behaviors. Here my comments show what we want to do in very basic terms, and also suggest some math that will solve the problem.
We can use Mathf.Abs to turn any number positive, and Mathf.Sign to get the positive or negative sign of a number. We can know to use these functions because that is what is required by the pseudocode.
Here is the end result. The square can only go so far up or down, and it will wrap around the screen if it goes right or left. The power of pseudocode made this very easy to implement without getting stuck for hours.