This article explains how to create pseudocode documentation for your programs. Pseudocode is nothing more than a description of your code expressed using plain text and not a programming language.
Steps
Part 1 of 3: Understanding the Basics of Pseudocode
Step 1. Learn what pseudocode is
This is a step-by-step description of your code that you can gradually transcribe into the programming language. Many programmers use it to plan the function of an algorithm before turning to the technical part of programming.
The pseudocode serves as an informal guide; it is a tool for reflecting on the problems posed by the program and a means of communication that helps to explain your ideas to other people.
Step 2. Learn why pseudocode is useful
This tool is used to show how an algorithm works. Programmers often use it as an intermediate step in programming, between planning and writing the actual code to execute. Other uses of the pseudocode include:
- Describe how an algorithm works. The pseudocode can illustrate where particular constructs, mechanisms or techniques are to be inserted in a program.
- Explain a computational process to a novice user. Computers need very strict syntax to run a program, while people (especially non-programmers) can better understand more fluid and subjective languages, which clearly explain the purpose of each line.
- Program in a group. High-level software designers often include pseudocode in their designs to help programmers solve complex problems. If you are developing a program with colleagues, pseudocode can be helpful in clarifying your intentions.
Step 3. Remember that pseudocode is subjective and has no standard
There is no syntax you need to use to write it, so it is a common professional courtesy to use standard structures that other programmers can easily understand. If you are creating a project on your own, pseudocode should primarily help you structure your ideas and put your plan into practice.
- If you are working on a project with other people, be it colleagues, assistants or non-technical collaborators, it is important to adopt at least some standard structure, so that everyone can understand your intentions.
- If you are taking a programming course at a university, campus or company, your pseudocode will likely be tested according to the "standard" you were taught. This standard often varies between different institutions and also from one teacher to another.
Clarity is one of the primary goals of pseudocode and can help you if you work using the most commonly used programming conventions. While transforming pseudocode into actual code, you need to transcribe it into a programming language, so it may be helpful to choose the structure of the description with this ultimate goal in mind.
Step 4. Focus on the main purpose of the pseudocode
It's easy to get back to writing with a programming language once you get used to it. Remember the purpose of the pseudocode, which is to explain how each line of the program works, and you will be able to stay focused as you create the document.
Part 2 of 3: Write the Pseudocode Well
Step 1. Use a plain text editor
You may be tempted to use a word processing program (such as Microsoft Word) or similar software to create a rich-text document, but pseudocode requires as little formatting as possible, because it has to be simple.
Plain text editors include Notepad (Windows) and TextEdit (Mac).
Step 2. Start by writing the purpose of the process
Dedicating a line or two to the purpose of the program will help you create the rest of the document and save you the trouble of explaining what the program is for to all the people who read your pseudocode.
Step 3. Write only one sentence per line
Each sentence of your pseudocode should express a computer action. In many cases, if the list of actions is structured correctly, each of them will correspond to a line of pseudocode. Consider making the to-do list in advance, then translating that list into pseudocode, and finally gradually developing the document into real code that can be read by a computer.
Step 4. Use spaces and indentations effectively
By leaving some space between the "blocks" of text you will be able to isolate the various sections of the pseudocode and by indenting different parts of each block you will indicate what the hierarchical structure of your document is.
For example, a section of the pseudocode explaining the insertion of a number should all appear in the same "block", while the next section (for example the one referring to the output) should belong to a different block
Step 5. Type the most important commands in uppercase if necessary
Depending on the requirements of your pseudocode and the environment in which you are publishing it, you may need to capitalize the commands that will be part of the actual code.
For example, if you use the "if" and "then" commands in your pseudocode, you could enter them as "IF" and "THEN" (for example "IF number of input THEN result of output")
Step 6. Write using simple terms
Remember: you are describing what the project will do, you don't have to summarize the code itself. This is especially important if you are writing the pseudocode as a demonstration for a client who is not programming savvy or as a novice programmer project.
You could even get rid of the programming commands altogether and just define the operations of each line. For example "If the input is odd, the output is Y", it can become "if the user enters an odd number, show Y in its place".
Step 7. Keep pseudocode tidy
The language you use to write the pseudocode must be simple, but you must still keep all the lines in the order in which they are to be executed.
Step 8. Leave nothing to the imagination
Everything that happens within the process must be fully described. The phrases of the pseudocode must be similar to simple expressions in Italian. This type of code usually doesn't use variables; instead, it describes what the program should do with real references, such as account numbers, names, and amounts of money.
Step 9. Adopt standard programming structures
Even if pseudocode does not have a precise standard, it will be easier for other programmers to understand your explanation if you follow a structure similar to that of existing (sequential) programming languages. Use terms like "if", "then", "while", "else" and "loop" just like you would in real code. Consider the following structures:
- if CONDITION then INSTRUCTION means that a certain instruction will be executed only when the required condition is met. "Instruction", in this case, indicates a step that will be performed by the program, while "condition" stands for a data that must meet certain criteria before the action is authorized.
- while CONDITION do INSTRUCTION means that the instruction is repeated as long as the condition remains true.
- do INSTRUCTION while CONDITION is very similar to the previous structure. In the first case, the condition is checked before the instruction is executed, while in the second it is the instruction that is executed first; consequently, with this syntax the INSTRUCTION is performed at least once.
- function NAME (ARGUMENTS): INSTRUCTION means that whenever a certain name is used within the code, it is an abbreviation for a certain instruction. The "arguments" are a list of variables that you can use to clarify the statement.
Step 10. Organize the pseudocode sections
If you've written a document with large sections defining others within the same block, you can use parentheses or other punctuation marks to put everything in order.
- Brackets: you can use both square (eg [code]) and curly braces (eg {code}) to contain very long sections of pseudocode.
-
When writing a program, you can add comments by typing "" on the left side of the comment (ex.
// This is a temporary step.
- ). You can use the same method when writing pseudocode to leave comments that don't fit into the programming text.
Step 11. Verify that the pseudocode is clear and easy to read
You should be able to answer the following questions once you get to the end of the document:
- Would a person unfamiliar with the process understand the pseudocode?
- Is the pseudocode written to be easy to translate into a programming language?
- Does the pseudocode describe the whole process, without leaving anything out?
- Does every name used within the pseudocode have a clear reference for the reader?
- If you find that one of the sections of the pseudocode needs reworking or does not clearly explain a passage that another person may forget, add the missing information.
Part 3 of 3: Create a Sample Pseudocode Document
Step 1. Open a plain text editor
You can use Notepad (Windows) or TextEdit (Mac) if you prefer not to install a new program.
Step 2. Define your schedule
Although it is not strictly necessary, you can start the document with a line or two that will clarify the purpose of the program immediately:
This program will ask the user for a greeting. If the greeting matches a specific phrase, the user will receive a reply; otherwise, you will get an error message.
Step 3. Write the opening sequence
The first command (i.e. the first action that the program should perform as soon as it is executed) should occupy the first line:
print greeting "Hello stranger!"
Step 4. Add the next line
Put a space between the last line and the next by pressing Enter, then create the next line of code. In this example, you should ask the user to enter a sentence:
print request for input press "Enter" to continue
Step 5. Add the action
In this example, the user will be prompted for a greeting:
print asks "How are you?"
Step 6. Show the user a series of answers
Again, after hitting Enter in this example, the user should see a list of possible responses:
show possible answers "1. Good." "2. Great!" "3. Not good."
Step 7. Request user input
The program will ask the user to enter an answer:
print input request "Enter the number that best describes your mood:"
Step 8. Create "if" commands for user input
Since you can select various answers, you will need to add more results based on the option you choose:
if "1" print answer "Great!" if "2" print answer "Great!" if "3" print answer "Up with life, honey!"
Step 9. Add an error message
In case the user chooses an incorrect answer, you should prepare an error message:
if input is not recognized print answer "You don't follow the instructions very well, do you?"
Step 10. Add all other parts of the program
Keep drafting the document by adding sections or refining the details so that anyone who reads it understands it. Using the example in this guide, the final document should look something like this:
This program will ask the user for a greeting. If the greeting matches a specific phrase, the user will receive a reply; otherwise, you will receive an error message. print greeting "Hello stranger!" print request for input press "Enter" to continue
print asks "How are you?" show possible answers "1. Good." "2. Great!" "3. Not good." print request for input "Enter the number that best describes your mood:" if "1" print answer "Great!" if "2" print answer "Great!" if "3" print answer "Up with life, honey!" if input is not recognized print answer "You don't follow the instructions very well, do you?"
Step 11. Save the document
Press Ctrl + S (Windows) or ⌘ Command + S (Mac), enter the file name, then click Save.