DEV Community

Cover image for Use Pseudocode for a more efficient (and headache-free) coding
Kinsta
Kinsta

Posted on • Originally published at kinsta.com

Use Pseudocode for a more efficient (and headache-free) coding

Ever heard of Pseudocode? It's a combination of simple language and programming elements that helps you represent your code.

Understanding it is a time-saving trick you can integrate into your process at any time - so you don't have to be stressed and frustrated when explaining your code to others.

Here's more about Pseudocode, how it's used, and why it's an essential tool for any developer.

What Is Pseudocode?

Pseudocode is a simplified way of explaining coding elements like algorithms, functions, or other processes, using a mix of everyday language and programming-like elements.

It’s called “pseudo” code because it’s not actually executable. Instead, it’s a way for humans to understand and plan out the logic in coding — to describe the steps of a program in a way that’s easy for humans to understand, while still being detailed enough to be rapidly converted into a specific programming language.

Here’s a simple example of pseudocode, in which we’re working out the basic logic to greet a visitor by name when they navigate to our site or app:

PROCESS GreetUser
    INPUT userName
    DISPLAY "Hello, " + userName + "!"
END
Enter fullscreen mode Exit fullscreen mode

As you can see, the above pseudocode isn’t written with syntax from any actual language or framework. Instead, it uses simple, universally understandable language and programming elements — like PROCESS, DISPLAY, and + — to stand in as syntax, making it simple for anyone to follow.

That’s one of the powers of writing pseudocode: By laying the code’s intentions out in a common syntax, you can jump all programming and skill-based language barriers.

How To Write Pseudocode

Pseudocode's flexibility comes from its lack of specific syntax, which means there's no single correct way to write it. While languages like Pascal and Basic provide specific pseudocode rules, you can use any language or terminology, as long as it's universally understood and the logic is clear.

Despite its open-ended nature, developers usually follow some basic pseudocode guidelines. Let's dive into these next.

Sequences

A sequence is a group of statements that are executed in a specific order. They’re used to perform or repeat a series of simple actions. Some familiar sequence commands commonly used in pseudocode include INPUT, SET, PRINT, READ, DISPLAY, SHOW, and CALCULATE.

Here’s an example of pseudocode that uses some of these commands:

PROCESS CalculateCost
    INPUT price, quantity
    SET cost = price * quantity
    PRINT "The cost is: " + cost
END
Enter fullscreen mode Exit fullscreen mode

This pseudocode defines a process called CalculateCost that takes in a price and quantity, multiplies them together to calculate the cost, and then displays the result.

Conditionals

Conditional statements allow a program to make decisions based on certain conditions, then direct the program to execute certain statements if a condition is met (or not met). IF-ELSE, IF-IF ELSE-ELSE, and CASE statements are frequently utilized in pseudocode.

Here’s an example showing an IF-ELSE script in pseudocode:

IF user = returning
    PRINT "Welcome back!"
ELSE
    PRINT "Welcome!"
Enter fullscreen mode Exit fullscreen mode

In the above example, we’re describing a process that shows a “Welcome back!” message to users who have visited before, but shows only “Welcome!” to new users.

Iterations

Iteration statements repeat a set of steps within a larger function or process. They’re often tasked to perform the same operation on multiple items in a list or to repeat a process until certain conditions are met.

Iterations are useful for repeating a set of steps multiple times and can be implemented using various types of loops, including FOR, WHILE, and DO-WHILE loops.

Let’s look at some pseudocode that uses a FOR loop to iterate through a list of numbers:

PROCESS PrintWholeList
    INPUT listOfNumbers 
    FOR each number in listOfNumbers
    PRINT number
    END FOR
END
Enter fullscreen mode Exit fullscreen mode

In the above pseudocode, our PrintWholeList process takes in a list of numbers and then iterates through the list, displaying each number on the screen. The FOR loop allows the process to repeat the PRINT command for each item in the list.

Alternatively, we could utilize the common pseudocode to accomplish the same as our above loop. In pseudocode, it’s more common to use the keywords REPEAT and UNTIL in place of DO-WHILE:

PROCESS PrintWholeList
    INPUT listOfNumbers 
    SET counter = 0
    REPEAT
    PRINT listOfNumbers[counter]
    SET counter = counter + 1
    UNTIL counter = length of listOfNumbers
END
Enter fullscreen mode Exit fullscreen mode

As shown here, we can switch out the names, keywords, and syntax pieces all we like. This is just one demonstration of pseudocode’s flexibility. The key is to ensure that the logic is stable while using names that are ubiquitous enough to be read by anyone.

You can see some of these constructs used in the pseudocode examples we’ll work with later on.

Pseudocode Examples and Translations

Here are some examples of pseudocode, along with their translations into executable code in various languages and frameworks.

PHP

To start, let’s write some pseudocode that’s meant to mimic the logic of adding up all the numbers in a given list:

PROCESS FindTotal
    INPUT listOfNumbers
    SET sum = 0
    FOR EACH number IN listOfNumbers
    SET sum = sum + number
    END FOR
    PRINT sum
END
Enter fullscreen mode Exit fullscreen mode

Our pseudocode logic follows these steps:

  1. Give the function a name.

  2. Get the list of numbers from the end user.

  3. Create a variable called sum to house the numerical total as it gets calculated.

  4. Iterate through every number in the list one by one, adding each number to the sum’s total.

  5. After all the numbers have been added, end the iteration (loop).

  6. Display the final sum obtained from adding all the numbers together.

  7. End the function.

Now that we know the logic of our function, we can translate this pseudocode into any other language or framework. Let’s see what it might look like translated into PHP:

function findTotal($listOfNumbers) {
    $sum = 0;
    foreach ($listOfNumbers as $number) {
    $sum += $number;
    }
    echo $sum;
}
Enter fullscreen mode Exit fullscreen mode

Node.js

Next, let’s write some pseudocode we can use to check what the current time is for our visitors, then send them the appropriate greeting based on their time of day:

PROCESS TimedGreeting
    GET userTime
    IF userTime > 6:00 + < 12:00
    PRINT "Good morning!"
    ELSE IF userTime > 12:00 + < 18:00
    PRINT "Good afternoon!"
    ELSE
    PRINT "Good evening!"
END
Enter fullscreen mode Exit fullscreen mode

Our pseudocode logic follows these steps:

  1. Give the function a name.

  2. Find the user’s time.

  3. If the user’s time is between 6:00 AM and 12:00 PM, show the message “Good morning!”

  4. If the user’s time is between 12:00 PM and 6:00 PM, show the message “Good afternoon!”

  5. For any other time, show the message “Good evening!”

  6. End the function.

Translated into Node.js, it might look like this:

function timedGreeting() {
    const userTime = new Date();
    if (userTime.getHours() > 6 && userTime.getHours() < 12) {
    console.log('Good morning!');
    } else if (userTime.getHours() > 12 && userTime.getHours() < 18) {
    console.log('Good afternoon!');
    } else {
    console.log('Good evening!');
    }
}
Enter fullscreen mode Exit fullscreen mode

Python

For our next example, let’s write some pseudocode to outline the logic for reversing a string (in this case, a sentence) that’s given to us by a user:

PROCESS ReverseString
    INPUT string
    SET reversed_string = reverse of string
    PRINT "The reversed sentence is: ", reversed_string
END
Enter fullscreen mode Exit fullscreen mode

Our pseudocode logic follows these steps:

  1. Give the function a name.

  2. Prompt the user for a string and accept what they provide.

  3. Store the value of the user’s string variable.

  4. Slice the stored string value to reverse it, and store the result in a new variable.

  5. Display the reversed string on the screen.

  6. End the function.

When translated to Python, it might look like this:

string = input("Enter a sentence: ")
reversed_string = string[::-1]
print("The reversed sentence is: ", reversed_string)
Enter fullscreen mode Exit fullscreen mode

In some cases, your actual translated code will be shorter than your pseudocode. That’s fine. As long as your pseudocode’s logic is sound and your translated code functions as it should, you can disregard the discrepancies in length.

💡 Got any other use-case idea for Pseudocode? Drop them below!

Top comments (0)