The Cheeky Monkey Media Blog
A few words from the apes, monkeys, and various primates that make up the Cheeky Monkey Super Squad.
Pseudocode Programming
September 1, 2015 / Treena BjarnasonBefore writing any actual code for a module or project, you should try to construct your code idea in a low language(Plain English), which is a good practice. Then, when you write the actual code, the steps will be clear and automatic to create. A great benefit to this is that you have now a comments section to add to your code so future programmers will grasp the code easily.
Let’s look at an example:
PROGRAM MakeDoubleDoubleCoffee: Organize everything together; Add coffee in coffee machine; Add water to coffee machine; Start coffee machine; Pour coffee into cup; Add two sugars; Add two creams; Serve; END
This is a straightforward program for making coffee. Now, what if you don’t like sugar? We need to add a condition into the Pseudocode for that:
PROGRAM MakeDoubleDoubleCoffee: Organize everything together; Add coffee in coffee machine; Add water to coffee machine; Start coffee machine; Pour coffee into cup; IF ( sugar is required ) THEN Add two sugars; ELSE don’t add two sugars; ENDIF Add two creams; Serve; END
What if we want a loop in this program? Let’s say we want to fill the cup until it’s full. I really love coffee…
PROGRAM MakeDoubleDoubleCoffee: Organize everything together; Add coffee in coffee machine; Add water to coffee machine; Start coffee machine; WHILE (coffee cup is not full) Do keep filling cup of coffee; END WHILE IF (sugar is required) THEN Add two sugars; ELSE don’t add two sugars; ENDIF Add two creams; Serve; END
There it is. A program that fills up your cup of coffee and adds sugar if requested.
Pseudocode is plain, simple, and easy. You don’t need a background in computer programming to understand; therefore allowing developers to bring non-programmers (like the clients) into the development stages and benefiting from their input on things and helping to formulate their own end product.
It isn’t a programming language, but it can be used with ANY programming language. Some languages are better for certain tasks. Using pseudocode is a good alternative to getting the work started and not wasting any development time.
Believe it or not,s there is a programming language that resembles pseudocode a lot. They are called “Behat” and “Cucumber” … weird names I know.
Let’s look a Behat. Behat is a Behaviour-driven development framework and works with PHP. The way the framework works is by writing human-readable sentences that outline the features and how it works, then writing some behavior behind these human sentences.
Here is an example taken from the Behat website:
Imagine you’re about to create the famous UNIX “ls” command. Before you begin, you describe how the feature should work:
Feature: ls
In order to see the directory structure, as a UNIX user, I need to be able to list the current directory’s contents.
Scenario: Given I am in a directory "test" And I have a file named "foo" And I have a file named "bar" When I run "ls" Then I should get: """ bar foo """
In the example above, we can see that it resembles pseudocode a lot. Cucumber is almost very similar.
Pseudocode and Behat are powerful approaches to programming and fun to use. Here are some pages to look at for more behat information:
- Behat homepage: http://docs.behat.org/en/v2.5/
- Behat Github download page: https://github.com/Behat/Behat
- Behat cheatsheet: http://blog.lepine.pro/images/2012-03-behat-cheat-sheet-en.pdf