How to Write Good Pseudocode
As developers and data scientists, we go through many stages, from getting an idea to reaching a valid, working implementation of it. We need to design and validate an algorithm, apply it to the problem at hand, and then test it for various input datasets.
In the initial state of solving a problem, it helps to eliminate the limitations offered by a specific programming language’s syntax rules when we design or validate an algorithm. By doing this, we can focus our attention on the thought process behind the algorithm and how it will (or won’t) work instead of focusing on our syntax.
Here’s where pseudocode comes to the rescue. We use pseudocode in various fields of programming, whether it be app development, data science or web development. Pseudocode is a technique used to describe the distinct steps of an algorithm in a manner that’s easy to understand for anyone with basic programming knowledge. Although pseudocode is a syntax-free description of an algorithm, it must provide a full description of the algorithm’s logic so that moving from pseudocode to implementation is merely a task of translating each line into code using the syntax of any given programming language.
Why Use Pseudocode?
- Better Readability: Often, programmers work alongside people from other fields such as mathematicians, managers and business partners. Using pseudocode to explain the mechanics of the code makes communicating between different specialties easier and more efficient.
- Eases Up Code Construction: When the programmer goes through the process of developing and generating pseudocode converting that into real code written in any programming language will become much easier and faster.
- A Good Middle Point Between Flowchart and Code: Moving directly from the idea to the flowchart to the code is not always a smooth ride. That’s where pseudocode presents a way to make the transition between the different stages somewhat simpler.
- Acts as a Starting Point for Documentation: Documentation is an essential aspect of building a good project but starting documentation is often the most difficult part of the process. Pseudocode can represent a good starting point for what the documentation should include. Sometimes, programmers even include the pseudocode as a docstring at the beginning of the code file.
- Easier Bug Detection: Since pseudocode is written in a human-readable format, it is easier to edit and discover bugs before actually writing a single line of code. We can edit pseudocode more efficiently than testing, debugging and fixing actual code.
The Main Constructs of Pseudocode
At its core pseudocode is the ability to represent six programming constructs (always written in uppercase): SEQUENCE, CASE, WHILE, REPEAT-UNTIL, FOR, and IF-THEN-ELSE. These constructs — also called keywords —are used to describe the control flow of the algorithm.
SEQUENCE represents linear tasks sequentially performed one after the other.
WHILE a loop with a condition at its beginning.
REPEAT-UNTIL a loop with a condition at the bottom.
FOR another way of looping.
IF-THEN-ELSE a conditional statement changing the flow of the algorithm.
CASE the generalization form of IF-THEN-ELSE.
Although these six constructs are the ones we use most often you can theoretically use them to implement any algorithm. You might find yourself needing more based on your specific application. Perhaps the two most needed commands are:
Invoking classes or calling functions (using the CALL keyword).
Handling exceptions (using EXCEPTION, WHEN keywords).
Of course, based on the field you’re working in, you might add more constructs (keywords) to your pseudocode glossary as long as you never use these keywords as variable names and ensure they’re well known within your field or company.
When writing pseudocode, everyone has their own style of presenting since humans are reading it and not a computer; pseudocode’s rules are less rigorous than that of a programming language. However, there are some simple rules that help make pseudocode more universally understood.
How to Write Pseudocode
- Always capitalize the initial word (often one of the main six constructs).
- Only one statement per line.
- Indent to show hierarchy, improve readability, and show nested constructs.
- Always end multi-line sections using any of the END keywords (ENDIF, ENDWHILE, etc.).
- Keep your statements programming language independent.
- Use the naming domain of the problem, not that of the implementation. For instance: “Append the last name to the first name” instead of “name = first+ last.”
- Keep it simple, concise, and readable.
Following these rules help you generate readable pseudocode and be able to recognize ones that aren’t well written.
Whether you’re a computer science major, went to bootcamp, or took a programming class, you’ve probably heard of pseudocode before. When I teach my students pseudocode, at first, they don’t see the use of it; they think it’s a waste of time. As they put it, “why write code twice?” That might be correct in the case of simple, straightforward problems. However, as the complexity and the size of the project increases, programmers come to realize how generating pseudocode makes writing the actual code much easier. Pseudocode helps you realize possible problems or design flaws in the algorithm earlier in the development stage, which saves you more time and effort on fixing bugs and avoiding errors down the road.
Pseudocode is an underestimated and under-utilized tool within the programming community, but a clear, concise, straightforward pseudocode can make a big difference on the road from idea to implementation — and a much smoother ride for the programmer.
This article was originally published on Towards Data Science.