Before we code, we pseudocode
The last tool of a responsible Programmer is a programming language!
PseudoWHAT?!
Pseudocode is a way to represent the code (like an algorithm) in a human-readable way that is programming-language agnostic. Pseudocode isn’t a programming language like JavaScript
or PHP
but is a simplified representation of the code you are writing.
While styles vary by Developer and may sometimes have code-like structures (like an if
statement), but it is always written in the Developer’s human language of choice (mine is English).
As you will see below, the purpose of pseudocode is to outline the logic of the program you are writing. It provides a clear and concise representation of how a program should function, helping Developers to visualize and refine their ideas before implementing the code.
By documenting your thought process, you are more likely to stay on track, and avoid the frustration of trying to code a problem you don’t understand (this can be especially detrimental to us neurodiverse folks with mood disorders).
More than just code
By writing out the logic to your program, your brain is free to focus on the program’s flow and logic. You are more likely to “get things right” the first time, and when you make mistakes you have the pseudocode to go back to and iterate. I also believe that by writing the pseudocode, you are more likely to catch mistakes.
Perhaps of equal importance is the creation of a shared language for collaboration- with your teammates, or Stack Overflow (if you are really brave and well prepared). I think that most beginners or even intermediate Developers don’t know about (or if they do, don’t utilize) pseudocode.
Prior to my journey at Perpetual Education(PE) I had no idea about pseudocode. As a school assignment PE challenged us to work through the amazing book Exercises for Programmers (EFP, linked in the resources section below). It was here that I learned how to identify and break down problems.
Programming as a word problem
I was never a student who enjoyed or succeed in mathematics. But this was because I had no idea how to break down problems and saw no real-world applications to the problems I faced in Math.
In retrospect, I often wonder how different my mathematical education would have been if I had learned about pseudocode. I say this because programming reminds me of mathematical word problems. more often than not, to solve a word problem you need to solve a whole bunch of smaller problems first.
When students encounter a word problem, they are encouraged to work it out with paper. Some tests even require you to show your work. This is to discourage cheating but also to increase the student’s chances of solving the problem.
Instead of trying to do it all in your head, writing out the steps takes you from worrying about the problem as one big problem, and instead seeing it as a series of smaller problems to be solved. It’s the difference between viewing the problem as a mountain vs seeing the path to climb it.
When we write pseudocode we create a route of problem-solving, a plan of action. I am reminded of “The French Spider Man“ Alaine Robert. Alaine is a rock climber who made the transition to climbing buildings using nothing more than a bag of climbing chalk and shoes.
Because Alain doesn’t have permission to climb these buildings, he has to plan months in advance (accounting for the weather too!) and get to the building at dawn to elude the authorities. Because he plans (and physically trains) for all variables in advance, like the weather and the climbing route he will take up the building, by the time the climbing day comes he is prepared. You could say this is a pseudocode of sorts!
And Alain doesn’t just climb any building. Like a Developer, he has to work with constraints- a type of limit to adhere to. He wont climb a building if it is structurally unstable or the weather is bad. Maybe us Developers aren’t that different from normal people after all ;)
While programming isn't usually a matter of life or death (at least for front-end development; embedded systems might present a different story), there are real-world consequences to neglecting pseudocode or omitting tests for your code, whether it’s providing example outputs or employing automated testing software.
One of the worst case scenarios as a Developer is releasing a piece of software to the world without it being thoroughly planned and tested. It may appear to work but be buggy, insecure or both and have undefined (unpredictable) behavior! The pseudocode system helps to prevent such a disaster from happening.
Understanding the problem
As Developers, we solve problems with programming languages. But before we can do that, we must understand the problem and identify the algorithm, inputs, and outputs to create a program with the desired behavior. This usually takes the form of a problem statement: a short description of the problem.
I will use a problem statement (and the associated pseudocode) I encountered recently while solving an Exercises for Programmers exercise. I also made a point to identify the inputs, outputs, processes, constraints and example tests too.
Write a program that asks for three numbers. Check first to see that all numbers are different. If they’re not different, then exit the program. Otherwise, display the largest number of the three
Writing the Pseudocode
Inputs
3 (or more) numbers
Outputs
The largest number of a group of numbers (3 or more), or error message saying “unique values only”
Processess
Find the largest number
Tests
Valid input
What’s your first number? 10
What’s your second number? 0
What’s your third number? 8
output: Your largest number is 10
Invalid input
What’s your first number? 32
What’s your second number? 32
output: You entered 32 twice, unique numbers only
Constraints
Write the algorithm manually. No built in functions allowed for finding the largest number
No repeated numbers (unique numbers only)
Require user to enter input and numbers only
Pseudocode (finally)
Create a form with a label and input for a user number. Include the proper HTML attributes(for, id, name, input type, placeholder, button type). Create a submit button
Get a reference to the form
add an event listener to the form and listen for the form submission. Upon submission
Get user input from the form and convert it to a number. Consider encapsulating this functionality in an object literal
Require user input, make it type=number
Create a
userNums
array to store the numbers. The array is sorted in ascending orderCreate a
saveNumber
function to push the numeric value from the form field to theuserNums
arrayCreate a
generateMessage
function to interpret the return values of the functions and output different results.Upon the first submission, first check if the
userNums
array is empty. If it is empty:Output `your number is
${userNum}
`.If it’s not empty, i.e. 2 array items: Upon second submission check if the number has been entered already and if it is larger than the previously entered number. If it has been entered already, disable the submit button and output `
${userNum}
has already been entered`If it hasn’t been entered already, output the result of seeing if it is the largest number in the sorted(ascending order) userNums array
Repeat the above logic until the largest number is found.
If it is found, disable the submit button. Require the user to reload the page or write a function to reload the page
The algorithm for finding the largest number. A couple of choices:
Use nested for loops, loop for the length of the array, and compare each element to see if it equals the next element
Use filter and indexOf. Something like
userNums.filter( ( ) => userNums.indexOf(userNum))
. The logic is: does an index exist in the userNums array that matches theuserNum
value from the form field? If true, it is a repeated number. If false, it is a unique number.
While it is important to spend as much time as necessary on the pseudocode, so is respecting the clock and avoiding burnout! You can always change the pseudocode if you find something doesn’t work. But that takes action! So don’t spend all your time planning without trying to implement your algorithm/code/whatever.
I took the pseudocode for the EFP comparing numbers exercise and attempted to solve the problem statement using the above pseudocode. I linked to it in the Code section below. Take a look at it and tell me what you think!