Unity Fundamentals — Pseudocode deciphering your thoughts
When you are writing code it can sometimes be hard to translate the ideas you have in your head directly into code. Luckily there is a trick programmers use to make this a little easier that trick is Pseudo-code.
Pseudocode what is it?
Pseudocode is the act of writing down functionality in plain English so it is easier to break it up into pieces of functionality and code. It is a very powerful tool in the arsenal of every programmer.
Pseudocode why use it?
Overview — Breaking up your functionality into pseudo-code gives a great overview of what needs to be done and what elements you need to get the desired result.
Ease of use — It is also much easier to write down your thoughts since it is in a form that is more familiar to you than code in most cases.
Clear — Writing long lines of code in one go can make it a lot more confusing to write but also debug when something goes wrong. Writing pseudo-code gives you a clear overview of what needs to be done and what you want to achieve.
Universal — Unlike code written in a specific language, pseudo-code is universal and can be translated into whatever language you decide to use. This makes it great for documenting functionality when still deciding which language will be used.
Pseudocode how to write it?
Think of it as a recipe when cooking, a recipe is written in clear and chronological steps the same goes for pseudo-code.
When writing pseudo-code there are a few things you can do to make it easier to turn into “real” code later on.
- Be Specific.
- Keep it simple.
- Write one statement per line.
- Indent your functionality so it's clear what belongs were.
How to turn it into actual code
Now you have written your code it is time to turn it into code in this case C#.
Let's take a look at the following example and how to go about doing this.
Now that we have our pseudo code written we can go over it line by line and turn it into code. The first line can best be achieved with an if statement so let's tackle this first. The indentation shows that the rest of the code is nested within this functionality so let's place it within the statement for now.
Now we can tackle the next part “Fire one bullet when ammo is not empty”, note that we will need 2 variables for this ammo and bullet, let's create these first.
Now we can work on the logic where we check if the ammo is greater than 0.
Lastly, we create the logic for actually creating the bullet by instantiating the game object we defined in the previous variable bullet
And that's it we have successfully turned it into functional code!
I know it can feel tedious when only using it for simple functionality like this but trust me when I say it will become immensely more valuable when your programs become more complex, so it really is good to start making this a habit from the beginning so it becomes second nature.
I hope this helps you not only write better code but most of all understand the code you are writing.
Thanks for reading and happy coding!