Unity Quick Tip - Pseudocode and why you should use it
What is pseudocode?
Pseudocode is the plain English translation of what we want to do in our code. Think of it as writing the “to do” list or a framework of what you want your code to do. We usually place it in our code as a comment (using “//” before our text) which we later delete as we are implementing the functionality the pseudocode refers to.
When writing code programmers have a simple yet powerful tool at their disposal. We can use pseudocode to “brute force’ the implementation of a function and then go back and optimize our code after we have the functionality working.
Think about this scenario: a programmer is tasked with making a function that allows the player to drink a health potion when a button is pressed.
If we break this function down into its components and write it in pseudocode first to describe what we need to do, we end up with a very convenient list of steps to take in order to get the function to work.
As you can see, we break it down into a few checks and other logic. Now we just start converting our plain English text into code.
We have only converted 2 of our lines to code and already the functionality is taking shape. As we are filling it out though we may come across an issue we need to solve, our check to see if the player is at max health…the variable for max health doesn’t exist, but now we know what we need , we can add that variable in.
Now our function is nearly complete. Next we convert the rest of our code over and leave a comment reminding us to add in an animation once one has been created.
There we go. We have a functioning system to drink potions, all written using pseudocode as a guide.
That’s all for now.