Objective: a description of pseudo code and its usefulness
I you want a coherent, precise, scientific definition… here is Wikipedia. My purpose is to show how pseudo code could be useful, especially when you are a beginner.
So what is it? Basically is a sort of meta language, in which you write computer instructions, floating between a programming and human language. You may find it on the web, on programming textbooks or even on code, as comments. In any case, it is used to clearly explain the algorithm, even to readers not knowing the specific programming language.
An approach I found useful, both during work and study, is to break a problem into little parts. I learnt it while studying physics, applied it when I was a researcher… but I found it useful in many other fields, ranging from everyday life problems to coding problems.
So, basically, I have a task or a problem to solve (now let’s consider coding), little or big, it can always be decomposed into other minor tasks/problems. For example: I want to build a Super Mario-like platform game. Let’s decompose it:
- Build the platforms
- Build the player
- Build the enemies
- Build power ups and coins
Each of them can be decomposed, for example let’s talk about the player
- Movement control
- Jump control
- Interaction with collectables
- Interaction with enemies
Again we can decompose… Got the idea?
But why is this related to pseudocode? Well, while facing a coding problem (let’s call it challenge ok? It’s cooler) you can write this down and have a rough idea of what you need and what you need to do. Here is an example concerning the power ups.
It may also be more specific and closer to a programming language.
This example is still quite understandable (less than before) but closer to code, with comparisons, value assignments etc. It is also clearer the algorithm to give player invincibility. This was a hard one, let’s look at this.
This would be a simple algo to implement the player death-by-fall, which would happen if the y position (2D game) would become lower than a fixed threshold value. The player loses a life and if the amount goes below zero: game over baby!
Now that the pseudo code is done, we would start actual coding, implementing the instructions in a real programming language (C# for Unity development). Once it is done, tested and working, all those comments must go. Even if they appear nice, someway clean and of course clear, always try to code as if your code would be manipulated later by some other colleague. So remember:
Clear the code of every pseudo code comment and, if possible, of every other comment explaining your code.
Code is like a joke: if you have to explain it… maybe it’s not that good.