It's time to ditch Medium for good! 🌈⚡️

Introducing Devblog by Hashnode. Blog on your domain for FREE. Highly customizable and optimized for developers.

Learn more

How To Write Pseudo-code

Have you ever had a really complex programming issue? An issue where you can probably write out the logic, but you aren't quite sure of the syntax you should use? Writing pseudo-code is a great place to start.

What it is

Pseudo-code is "language" where you can write all of your coding logic without writing one line of language-specific code. You see this a lot in algorithm research, especially machine learning algorithms. That doesn't mean you can't use it for web development.

Why you would use it

There are projects that are so massive that if you don't take the time to write a little pseudo-code, you could end up lost in a sea of implemented code. When you write some pseudo-code, it gives you a chance to really think through potential issues. You're able to look at pure logic and program flow without worrying about how your code runs.

Writing pseudo-code before you start typing real code will also help you finish your projects faster. Think of it as a blueprint. You know where everything needs to go and how everything works together. So when you get to the actual building phase, you don't have as much to think about because you've already thought through what you need to do.

The best part is that pseudo-code doesn't depend on any programming language. That logic you just wrote out can be taken by anyone and translated into their language of choice. It gives you the freedom to reuse and improve the architecture of the application that you're building.

One of the more subtle uses of pseudo-code is to share it with other people. Sometimes you'll have a specific piece of logic that can be used across multiple projects, but they are all in different programming languages. When you have the pseudo-code available, you can give it to other programmers and they can write that logic in whatever language they need to.

Another great feature is that you can write pseudo-code in any format you like. You could use the academic format. It's incredibly structured and detailed, but it tends to have a lot of math involved. Or you can write out a simple outline of what you expect your code to do.

How to write it

Here's an example of some pseudo-code I wrote in one of my academic papers:

pseudocode-ex.png

I'll be the first to admit that this is probably overkill for web development. If you find yourself using LaTex to write your pseudo-code, you might be making it more complicated than you need to. Odds are strong that a quick little write up in Word or even Notepad will be sufficient.

Here's an example of some simple pseudo-code I wrote for one of my web development projects:

IF userlogin = true
    API call to get user data
    Assign data to variables
    Re-route user to dashboard
ELSEIF userlogin failed more than 3 times
    Don't allow more attempts
    Send user notification email
    Re-route user to home page
ELSE
    Log bad login attempt
    Show error message
    Clear login form

You don't have to be super technical with your pseudo-code, but typically the more detail you can pack into it the easier it is to write the actual code. Think about it like you're writing an outline for your program. It gives you a chance to really think through what you are trying to accomplish and you can see exactly how all of the code comes together.

The main things you need to focus on with pseudo-code are:

  • The logical flow of your program
  • The details of the complex parts of your program
  • A consistent format

It really doesn't take much to write pseudo-code besides some hardcore thinking. As you write it, you'll start to see places where you could add more detail or places where you can take away some detail. Remember, this is mainly for your use so write it in a way that makes sense to you.

Personally, I love pseudo-code. It helps me keep my train of thought clear when I start typing the real code. When you have all of your logic already planned out, you get so much more time to experiment with performance and optimization. On top of that, you don't have to think as hard when you're deep into the code writing (yay for planned laziness).

What do you think? Do you think pseudo-code is worth the time or would you rather just jump into the code?


Hey! I know some of you are struggling with understanding what happens on the back-end of development and that's ok. On Apr. 28th, I'm hosting a live webinar where I'll teach you about the .NET Core framework and C#. It'll only take an hour and by the end of it you'll have a solid understanding of classes and an app you can show people!

If you want to learn all this stuff for free, go register for the webinar by clicking here. Registration ends Apr. 27th so don't miss your chance to learn something important for free!

Milecia's photo

Milecia

Write your comment…

Be the first one to comment