What's a Pseudocode?
When working on a software solution (algorithm, apps…) with teammates, you need a way to quickly express your thoughts and ideas. This is where pseudocode comes to the rescue. As you can guess, pseudocode is a kind of code that is free from language-specific syntax (PHP, Python, JS…). In other words, pseudocode is simply the description of your algorithm in
Let’s see how you can benefit from pseudocode before seeing a concrete example with a bubble sort algorithm.
Benefits
There are many benefits to using pseudocode:
- Helps to reinforce your understanding of a solution before you implement it
- Easy to share and explain, as it is written in human language and not code
- No constraints: you can use any format you want
- Programming-free language: a solution of an algorithm written in pseudocode can be understood by all developers, even if they don’t use the same programming language.
- And more!
Implementation
Now is the time for hands-on practice (but this is the section you were actively looking for anyway, isn’t it?)
The purpose here is to write pseudocode for the
function bubbleSort(arr)
Set isSwapped to true
WHILE isSwapped = true
Reset isSwapped to false
FOR each item in the arr
IF current item > next item
swap items
Reset isSwapped to true
ENDIF
ENDFOR
ENDWHILE
RETURN arr
As you can see, I just explained my logic flow so that you can easily implement it in your preferred language.
I have used the function keyword here to let you know that it is a function, though other people may use the procedure keyword. Every time I use a endBloc at the end (they are called scope terminators), and I capitalize them.
I also use indentation as much as I can, and I suggest you do so as well (for readability).
Better to know
The main rule for pseudocode is that there are no rules at all. Any rules in place will can depend on the school or company you are writing for.
Just make sure:
- your logic flow is clear and easy enough to be understood by anyone
- you’re consistent on the format you choose to use
Don’t be astonished if you see pseudocode following the format of a given language. > For example, a C developer may naturally format his pseudocode following the C coding style.
The main thing is that your pseudocode must be easy to consume by almost everyone without much effort; however, this should not keep you from getting creative, if you want!
Some keywords to use
Below are some keywords to give you inspiration.
Closing notes
Pseudocode is a plain
There’s no formal rule for pseudo-coding, rather it depends on your personal style, school, or company. The most important thing is consistency in your chosen style. You can make use of keywords such as Do While...EndDo, If...Endif, and Return.