Introduction to Programming for Laymen
You have now seen snippets of source code from different programming languages and surely realized the syntactical differences. How differently comments and other programming constructs can be written depending on language.
For learning programming concepts like flow control statements or discussing algorithms, computer scientists or software designers sometimes use what we call pseudocode.
Pseudocode is simply the use of a natural language (typically English) to describe logic or algorithms in way that may make it easier for us humans to read.
It's also used by designers to describe the intended functionality independently of what language(s) the solution may be later implemented in, or implemented in multiple languages, for different applications or devices.
Examples
IF (monthly salary is larger than monthly expenses ) THEN
oh yeah, we can save!
DO
invest money
any new money from interest? to reinvest
WHILE (still have something left to save)
ELSE
alert that we can't save anything
END IF
FOR (every month in a year)
show our budget, how much we saved this month
END FOR
Especially the explicit use of END IF, END FOR, etc differentiates pseudocode from most modern programming languages. Programming languages often use (e.g.) '{' and '}' to create statement blocks (previous chapter) and doesn't have specific END IF etc, and never with spaces like in 'END IF'.
Guidelines
A few guidelines found Oct 2011:
- Mimic good code and good English. Using aspects of both systems means adhering to the style rules of both to some degree. It is still important that variable names be mnemonic, comments be included where useful, and English phrases be comprehensible (full sentences are usually not necessary).
- Ignore unnecessary details. If you are worrying about the placement of commas, you are using too much detail. It is a good idea to use some convention to group statements (begin/end, brackets, or whatever else is clear), but you shouldn’t obsess about syntax.
- Don’t belabour the obvious. In many cases, the type of a variable is clear from context; unless it is critical that it is specified to be an integer or real, it is often unnecessary to make it explicit.
- Take advantage of programming shorthands. Using if-then-else or looping structures is more concise than writing out the equivalent in English; general constructs that are not peculiar to a small number of languages are good candidates for use in pseudocode. Using parameters in specifying procedures is concise, clear, and accurate, and hence should not be omitted from pseudocode.
- Consider the context. If you are writing an algorithm for quicksort, the statement use quicksort to sort the values is hiding too much detail; if we have already studied quicksort in class and later use it as a subroutine in another algorithm, the statement would be appropriate to use.
- Don’t lose sight of the underlying model. It should be possible to “see through” your pseudocode to the model below; if not (that is, you are not able to analyze the algorithm easily), it is written at too high a level.
- Check for balance. If the pseudocode is hard for a person to read or difficult to translate into working code (or worse yet, both!), then something is wrong with the level of detail you have chosen to use.
(author: Naomi Nishimura)
Introduction to Algorithms Summer 2003 - www.cs.cornell.edu/Courses/cs482/2003su/ (local PDF) |
More Resources
Updated
2013-01-05