UPDATED BY
Matthew Urwin | Mar 05, 2024

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.

What Is Pseudocode?

Pseudocode in data science or web development is a technique used to describe the distinct steps of an algorithm in a way that’s easy for anyone with basic programming knowledge to understand.

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.

 

What Is Pseudocode?

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.

Clarify Your Code5 Ways to Write More Pythonic 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.

  1. SEQUENCE represents linear tasks sequentially performed one after the other.
     
  2. WHILE is a loop with a condition at its beginning.
     
  3. REPEAT-UNTIL is a loop with a condition at the bottom.
     
  4. FOR is another way of looping.
     
  5. IF-THEN-ELSE is a conditional statement changing the flow of the algorithm.
     
  6. CASE is the generalization form of IF-THEN-ELSE.
pseudocode constructs

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:

  1. Invoking classes or calling functions (using the CALL keyword).
     
  2. Handling exceptions (using EXCEPTION, 5WHEN keywords).
extra pseudocode constructs

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.

More from Sara A. Metwalli5 Git Commands That Don’t Get Enough Hype

 

How to Write Pseudocode

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 those of a programming language. However, there are some simple rules that help make pseudocode more universally understood.

How to Write Pseudocode

  1. Always capitalize the initial word (often one of the main six constructs).
  2. Make only one statement per line.
  3. Indent to show hierarchy, improve readability and show nested constructs.
  4. Always end multi-line sections using any of the END keywords (ENDIF, ENDWHILE, etc.).
  5. Keep your statements programming language independent.
  6. 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.”
  7. Keep it simple, concise and readable.

 

Pseudocode Example

Following these rules helps you generate readable pseudocode and be able to recognize ones that aren’t well-written.  

pseudocode example

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.

Be a Better Data Scientist4 Data Science Portfolio Projects You Need to Create

 

Why Use Pseudocode?

Pseudocode Is Easier to Read

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.

 

Pseudocode Simplifies 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.

 

Pseudocode Is 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.

 

Pseudocode Is  a Helpful 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.

 

Pseudocode Allows for Quick 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.

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.

 

Frequently Asked Questions

What is pseudocode?

Pseudocode is a representation of code used to demonstrate the implementation of an algorithm without actually doing so. It often acts as a rough draft of coding projects, and is written in an explainable manner to be understandable by programmers at any knowledge level.

How do you write a pseudocode?

Some general guidelines for writing pseudocode include the following:

  • Make only one statement per line
  • Capitalize the initial word of a line (which are often pseudocode constructs) 
  • Use indentations to show hierarchy and nested constructs
  • Use the naming domain of the problem, not of the implementation 
  • Keep statements simple, readable and programming language independent
     

At the same time, pseudocode writing style can vary for each programmer, so it may not always include these guidelines.

What is an example of pseudocode?

Consider an online store that offers discounts on orders of $50 or more. Following the principles of writing good pseudocode, the pseudocode for an algorithm determining if a user gets a discount could go as follows:

  • IF price of an order is equal to or greater than $50
    • Apply discount
    • Display message: “Discount has been applied!”
  • ELSE 
    • Do not apply discount 
    • Display message: “Add more items to cart for discount!”
Expert Contributors

Built In’s expert contributor network publishes thoughtful, solutions-oriented stories written by innovative tech professionals. It is the tech industry’s definitive destination for sharing compelling, first-person accounts of problem-solving on the road to innovation.

Learn More

Great Companies Need Great People. That's Where We Come In.

Recruit With Us