What is pseudocode?
Pseudocode is a detailed yet readable description of what a computer program or algorithm should do. It is written in a formal yet readable style that uses a natural syntax and formatting so it can be easily understood by programmers and others involved in the development process. Pseudocode is not a programming language and cannot be compiled into an executable program. Instead, it serves as a blueprint for translating the code's logic into an actual programming language.
When pseudocode is incorporated into the development process, designers, lead programmers and other key players can use the code to design, collaborate on and evaluate the logic behind the program or algorithm. Pseudocode also provides programmers with a detailed template for writing code in a specific programming language.
Because pseudocode is written in readable format, it can be inspected by a team of designers and programmers as a way to ensure that the actual programming will match design specifications. Catching errors during the pseudocode stage is less costly than catching them later in the development process. Once the pseudocode is accepted, it can be translated into the vocabulary and syntax of a programming language. In some cases, the same pseudocode might be turned into multiple programming languages.
How do I write pseudocode?
There is no one approach to writing pseudocode, nor is there a single set of rules or agreed-upon standard for how to create pseudocode. It can vary widely from one source to the next, differing in both structure and syntax. Pseudocode is meant only as a tool to help expedite the development process, and as such, should make that effort easier and more efficient. It should not add another layer of complexity to the process.
Some pseudocode is extremely formal, with precise formatting and syntax, looking much like a real programming language. Other pseudocode reads more like standard prose, simply describing what is supposed to happen one statement at a time. The rest falls somewhere between these two camps, with people using whatever approach works for their current situations. In some cases, an organization will adopt its own pseudocode standards for internal development efforts.
The primary prerequisite for pseudocode is that it's comprehensible to the people who need to understand it, no matter what the code's structure or syntax is. For example, the following pseudocode describes a simple script that will ultimately be translated into Python:
This script will retrieve a list of subdirectories from the target directory and list the number of items in each subdirectory. SET target = target directory SET dir_list as empty list FOR each item in target SET full_path = target + item IF full_path is directory SET size = number of children in full_path IF size = 0 APPEND full_list with "<item> contains no items." ELSE IF size = 1 APPEND full_list with "<item>: <size> item." ELSE APPEND full_list with "<item>: <size> items." ENDIF ENDIF ENDFOR SORT full_list PRINT full_list with line breaks
The pseudocode begins with a short introductory statement that describes the script's purpose. Not everyone includes an introductory statement, but it can be useful in explaining what the code does so it can be more easily and quickly understood. The statement is followed by the pseudocode itself, which describes the script's logic, using the following conventions:
- The pseudocode uses plain language mixed with keywords that convey specific constructs, such as an iteration FOR statement or conditional IF…ELSE statement.
- Each new statement and statement element starts a new line.
- Keywords that introduce some type of construct are in uppercase, as in SET, FOR, IF, SORT and PRINT.
- Code blocks such as conditional statements (e.g., IF…ELSE statement) or iteration statements (e.g., FOR statement) are closed with an "END" keyword, as in ENDFOR and ENDIF.
- The elements within code blocks are indented according to the logic flow. For example, a code block embedded into the outer code block is indented, as are the elements within the embedded code block.
- Syntax and formatting are consistent throughout, such as using SET for all variable assignments.
The pseudocode shown here is just one example of the various ways pseudocode can be structured. Development teams take many different approaches, such as not using uppercase for keywords or not including the "END" keywords. Regardless of the conventions used, the code must be specific enough so that the logic is clear from beginning to end. Anyone reviewing the pseudocode or building a program from it should be able to fully understand what each statement is trying to achieve.
After the pseudocode has been finalized, a programmer can then develop the actual code in a programming language. For example, the previous pseudocode example can be easily translated into the following Python script:
# import the os module import os # set target directory and directory list target = '/users/abcd/documents/testdata/' dir_list =  # loop through items in target directory for item in os.listdir(target): # set the full path for current item full_path = os.path.join(target, item) # include only subdirectories if os.path.isdir(full_path): # set the number of items in current subdirectory size = len(os.listdir(full_path)) # append info about each subdirectory to dir_list if size == 0: dir_list.append(item + ' contains no items.') elif size == 1: dir_list.append(item + ' contains 1 item.') else: dir_list.append(item + ' contains ' + str(size) + ' items.') # sort dir_list dir_list.sort() # print each item in dir_list on separate line print(*dir_list, sep='\n')
If you compare the Python script to the pseudocode, you can see that each Python statement corresponds to a pseudocode instruction. The script loops through the items in the target directory and then lists each subfolder in the directory, along with the number of child items in that subfolder.
It is possible to write a program that will convert pseudocode into a programming language. However, the pseudocode's syntax should be standardized enough for the translator to understand all the pseudocode statements each time the program is used.