loading...
Cover image for Moving Past Tutorials: Pseudocode

Moving Past Tutorials: Pseudocode

aspittel profile image Ali Spittel ・5 min read

An often overlooked part of coding is pseudocode. Pseudocoding involves thinking about the steps to solving a problem and writing them in plain words instead of any running programming language.

You may have run into the "staring at a blank text editor file with no idea where to start" problem, which can often be solved by writing pseudocode and really thinking through the problem at hand. If at any time you feel stuck when writing code, you will probably want to start writing some pseudocode.

First, in order to write pseudocode, we need to start thinking about the parts of the problem. Since computers are so literal, we need to think of every tiny detail and include it -- if we're skipping things then we'll probably have issues down the road.

First, let's think of the steps for taking out my dog.

- I have to find my shoes.
    - If my dog steals said shoes, I have to retrieve them
    - If it's cold or I'm going on a long walk and I'm wearing sneakers or boots, I need to find socks.

- If I need socks, I put one sock on my foot and then the other on my other foot.
- Then, I put the left shoe on my left foot and then the right shoe on the right foot.
    - If they have laces, I tie each shoelace.

- I find Blair's leash, poop bags, and my keys 
    - they're usually on the counter, but sometimes she will move her leash to somewhere else and I'll have to go looking

- I clip on her leash. I then keep holding onto her leash

- I open the door to my apartment, then close it, then lock it.

- We walk to the elevator, press the down button and then wait.

- When the elevator door opens, I walk in and also make sure Blair follows me in. If she doesn't I need to pull her in or pick her up and carry her

- We hit the button to go down to the lobby of our building, wait for the elevator to get to the lobby, and then exit the elevator and the building

Etc.

I could keep going, but I think you get it at this point -- there are a couple things to note here: I'm super specific. I am writing down every possible step. I also write about the things that could go wrong -- what are the edge cases that may happen? And how do I solve those?

But, I can refactor this to look even more like real code as a second step!

If it's cold:
    wear boots
If I'm going for a long walk:
    wear sneakers
If my shoes are not at the door:
    find them
else if my dog steals my shoes:
    chase her down and take them back
If I'm wearing sneakers or boots:
    get socks out of the drawer
    put one sock on my foot and then the other on my other foot
put on left shoe
put on right shoe
if the shoes have laces:
    tie the left shoe
    tie the right shoe

If Blair's leash, poop bags, and my keys are on the counter:
    grab them
else:
    find them in my apartment

Clip on Blair's leash
Hold on to her leash

Open the door to my apartment
Walk through it with Blair
Lock the door

Walk to the elevator
Press the down button
Wait for the elevator until it arrives

When the elevator arrives, get into it
Make sure Blair gets on too.

Press the button for the lobby
Wait for the elevator to get to the lobby
Exit the elevator
Exit the building

This still isn't code, and we'll need to figure out functions and organization in the future, but we have a starting point. We aren't looking at a blank text editor anymore, and a lot of this can be pretty easily translated over to actual code -- like the conditionals.

For Code Challenges

Of course, walking my dog isn't a very common program. Let's talk about what this may look like for a real code challenge -- say reversing a string.

The first thing I might do is try to solve the problem on paper a few times to see how I would do it on paper -- I even have a whiteboard by my desk for this exact reason!

So, in this case, I'd notice that I take the last letter of my word, and add it to my reversed string. Then my second to last one, and so forth until there aren't any other letters left to add to the reversed string. But, there's also another way: I can instead take the first letter from the string and add it to the end of my new new string, effectively doing the same thing. So, for "hello, world" the progression would look like this:

h
eh
leh
lleh
olleh
 olleh
w olleh
ow olleh
row olleh
lrow olleh
dlrow olleh 

Now that I've solved the problem on paper, I need to write the pseudocode for it. If I'm doing the same thing repeatedly in my code, I know that I need to use a loop -- and I'm definitely doing the same thing to multiple letters in this scenario.

So, the pseudocode may look something like this

reversed_string = ''
for letter in my_string
    reversed_string = letter + reversed_string

If you need more intermediary steps, we could write something out like this:

reversed_string = my_string[0] + reversed_string
reversed_string = my_string[1] + reversed_string
reversed_string = my_string[2] + reversed_string
reversed_string = my_string[3] + reversed_string

At which point, you might notice the pattern and be able to see the loop from there! You can always start out with more procedural code and add in abstractions and logic from there to DRY up the code and make it closer and closer to its final form.

Pseudocode for Applications

So, in the real world, you're probably going to be creating full applications or adding features to applications, not solving code challenges all day. In that case, after outlining your prospective features, like we talked about in the last post, you may take one feature at a time and write pseudocode for it.

For example:

User clicks on a button:
    A modal pops up with a form that has the fields name, address, and phone number

When the user submits the form:
    an AJAX request is sent to the server with the values from the form
    the modal closes
    if the form submission was successful:
        show an alert at the top of the page that says a new value was added
    else:
        show an alert with the errors

Do one feature at a time, not the whole application so that if one specification changes, your work wasn't in vain!

My Challenge to You

Pick a code challenge and write pseudocode for it. Once you're done, take the project from last time and write pseudocode for one of its features.

Conclusion

Pseudocode is a very helpful tool for starting to think through problems. It looks different for everyone, for some people, it looks like a programming language, for others it looks more like non-programming language. It can even look different from that! I'll sometimes just write my function names before starting to dive into code in order to write an outline for myself! Find what works for you.

If you're looking at a blank text editor screen with no idea where to start, pseudocode is a great place to start, or even a step back from that may be to draw out the process on paper, observing your steps in detail.

Posted on by:

aspittel profile

Ali Spittel

@aspittel

Passionate about education, Python, JavaScript, and code art.

Discussion

markdown guide
 

Love all the different examples. And the reminder to consider edge cases. It's easier to write pseudo code for an algorithms problem than for an application, showing both is a great way to see how you get from here to there :)

 

Interesting! I think it's really important to learn how to get past the blank editor problem - there's nothing more intimidating than a blank sheet of paper. I've not tried doing it with pseudocode.

I rely on writing a test first - identifying one behaviour of the program I want to write and imagining that I've already written it. What does it look like? What will it do? What function gets called? With what? What will it return?

For me, a test gets me solving one small problem, gets something down on the page and stops me fretting about everything else that's going on in the code/in my life/ on TV.

Does your pseudocode have an afterlife? Does it be become comments or do you tend to bin it once it's been replaced by code?

 

I think that's a really natural progression, and I normally write function declarations or tests first too. But, for beginners/jr devs, I think writing more traditional pseudocode is super helpful. It depends on the program, but I am normally minimalist on comments for simple code, maximalist on longer form documentation.

 

for your afterlife question, as it was recommended by steve mcconnell( software engineering expert ) Once the pseudocode is written, you build the code around it and the pseudocode turns into programming-language comments. This eliminates most commenting effort.
As a result the comments will be complete and much more meaningful.

 

I really like this write up Ali. I’ve done pseudo coding without knowing it actually had a term 😂. Mines is not as organized like your examples above, it’s more scatter brain but I try to outline what I may need to accomplish a task in bullet points or sentences.

 

Nice, I've always loved writing pseudo code as a starting point for programs or functions, usually just a piece inside the comments section, which really serve as a focus and guideline for what needs to be done. (And proof that you know what to do :))

Maybe one day all code will be written in pseudo code...mmm

 

Pseudo code is the first thing I've learn when I started my software programming course in school it's so useful for later!

 

Interestingly, way back in the Jurassic of computing (1970s) we were taught to always begin with pseudo-code; this was primarily because the means of getting the computer read our programs was punch cards, but it still makes sense.

 

Having started to properly learn functional programming I noticed that pseudo code leads very naturally to finished code. For example your initial reversed_string pseudo code is almost a classic form of recursion as it stands.

 

great article, couldn't agree more.
pseudocode is definitely a very important practice, a by-product of which are great comments in the code.
if you write a concise version of pseudocode inside the code and then add the relevant code beneath it, you've just got yourself comments that will help you (or anyone else) who will need to maintain this bit of code in the future

 

I remember once during my graduation project, which was for a real client, I got stuck on some security bug for two whole weeks! One day at the office as I was panicking about lost time and the looming deadline, I grabbed a notepad and pen and tried to think it through. Lo and behold in just 10 minutes, I found the logical issue by writing out pseudocode and rewriting it and going through the logic in detail.

Needless to say I learned my lesson the hard way. Since then the very first thing I do before I write any code is write out the logic of the program in comments as pseudocode, and more complicated things I start them off on paper.

If you're stuck, pick up a pen! You won't regret it.

 

I love it! I write articles and focus on the problem solving part rather than a specific programming/scripting language.

 

This post is a game changer for me. Thanks Ali !

 
 

Thanks for reminding us this basic but powerful concept!
Good bye blank text editor :-)

 
 

I think it's pragmatic for solving leetcode. I'd try it when I challenge them.

 

Thanks for this, always on point..