If you want to bake a loaf of bread, and you pull out the recipe card, the directions won't begin with "make bread". Of course, we're going to make bread but the point of a recipe card is to provide guidance as to how to make bread. It's broken down into simple parts, and as you complete each step, the dough starts to take form. The same can be said about pseudocoding.
What is pseudocode?
Pseudocode is a list of steps that illustrates how to achieve the desired result. The steps are written in plain natural language so they can be read and understood easily and can be translated into any programming language. You should pseudocode as if you're walking yourself through the objectives of the problem. It may seem tedious to pseudocode for simple logic, but it provides good habit-forming practice and will certainly help when constructing more complex problems.
Let's write some pseudocode to find the sum of a range when given an array of two numbers, and we want the range to include the starting and ending numbers.
First, it's helpful to identify the input(s) and expected output(s), including the expected datatypes, as well as any constraints or edge cases that may require special handling.
Next, you can start writing the logic for how you want to achieve the desired output, with each step serving as a stand-in for a line of code. If a line of pseudocode becomes lengthy, you may want to take a second look to see if it should be broken down into smaller steps.
Once you have a solid list of instructions, then you're finally ready to write the code, and you're on your way to a fully baked solution. Now for the fun part! Let's go ahead and fill in the spaces we left below each line of pseudocode with actual code.
Now you might be thinking that sure looks like a lot of pseudocoding for a short amount of code, and you're right, but maybe you've written some code but didn't account for a specific edge case. With the pseudocode steps included as comments, anyone is able to follow your thought process and can easily make suggestions. It's tempting to want to start to code immediately but we'll save a lot of time in the end by setting ourselves up for success from the start.
Top comments (0)