Pseudocode

Pseudocode

59.1 Purpose

Pseudocode is a tool for planning, defining, or documenting the contents of a program routine or module. As the name implies, pseudocode is similar to (and often based on) real code.

59.2 Strengths, weaknesses, and limitations

Pseudocode is an excellent tool for planning or designing program logic and computational algorithms. Because it resembles real code, programmers find it easy to use and to understand. Pseudocode is not a good tool for describing control structures, particularly when several nested decisions are involved. Because pseudocode resembles a programming language, it is not well suited to planning or designing manual procedures.

In part, because pseudocode so closely resembles real code, some designers tend to write (rather than plan or design) the code. Writing the code twice (once in pseudocode and once in real code) is a waste of time. When the designer worries about coding details, crucial design considerations can easily be overlooked. Also, programmers resent such over specification.

Even if the pseudocode is well done, programmers sometimes resent it. Specifying algorithms in what is essentially a high-level language limits the programmer’s flexibility. Additionally, the programmer can fail to distinguish between the analyst’s coding technique and the analyst’s design, and the result may be criticism (even rejection) of a perfectly good design based on inappropriate criteria.

59.3 Inputs and related ideas

Before writing pseudocode, the designer must understand the algorithm or procedure. The necessary information might be compiled from direct observation, extracted from existing documentation, or derived from the problem definition (Part II) and/or analysis (Part IV) stages of the system development life cycle.

Other tools for documenting or planning routines or processes include logic flowcharts (Chapter 55), Nassi-Schneiderman charts (Chapter 56), decision trees (Chapter 57), decision tables (Chapter 58), structured English (Chapter 60), and input/process/output (IPO) charts (Chapter 64). A pseudocode routine usually exists in the context of a larger program. Tools for documenting or planning program structure include structure charts (Chapter 63) and HIPO (Chapter 64). The basic software logic blocks are defined in Chapter 62.

59.4 Concepts

With pseudocode, such details as opening and closing files, initializing counters, and setting flags are explicitly coded, but language-dependent details (such as the distinction between subscripts and indexes or the difference between real and integer numbers) are ignored. The idea is to describe the executable code in a form that a programmer can easily translate into real code.

There is no standard pseudocode; many different versions exist. Most, however, capitalize key words and operations and use indentation to show the logical relationships between blocks of code.

59.4.1 Sequence

Perhaps the easiest way to define sequential logic is by coding an algebra-like expression, for example,

      COUNT = 0



or




      STOCK = STOCK + QUANTITY



Such details as the sequence of operations and the rules for using parentheses should be consistent with the language to be used for writing the actual code. Data names should be taken from the data dictionary.



Input and output instructions are explicitly defined in pseudocode; for example,




      READ data FROM source



and




      WRITE data TO destination



where data is a list of variables, a data structure, or a record, and source and destination refer to a file or a database.



59.4.2 Blocks of logic


A block can contain any set of sequence, decision (selection), and/or repetition (iteration) logic. Once defined, the block’s instructions can be referenced by coding a PERFORM instruction:




      PERFORM block



For example, the instructions




      COUNT = 0
ACCUMULATOR = 0



might be assigned the block name INITIALIZE. Subsequently, the instruction




      PERFORM INITIALIZE



executes all the instructions in the block.



To distinguish between formal subroutines and internal procedures, some analysts use:




      PERFORM block USING list



for a subroutine, where list designates a list of variables passed between the calling routine and the subroutine.



59.4.3 Decision or selection


The general form of a decision (selection) block is:




  IF condition
THEN
PERFORM block-1
ELSE
PERFORM block-2
ENDIF



For example,




  IF HOURS > 40
THEN
PERFORM OVERTIME
ELSE
PERFORM REGULAR
ENDIF



By convention, the THEN block is executed if the condition is true and the ELSE block is executed if the condition is false.



Note the ENDIF. A feature of most pseudocodes is that each block of logic is clearly delimited. A decision block always begins with IF and ends with ENDIF, so there is no ambiguity. Note also the use of indentation. It makes the block easy to read.



It is possible to nest decision logic. For example,




  IF condition-1
THEN
IF condition-2
THEN
PERFORM block-a
ELSE
PERFORM block-b
ENDIF
ELSE
PERFORM block-c
ENDIF



Note how indentation highlights the relationship among these instructions. Note also how IF and ENDIF clearly delimit both decision blocks.



59.4.4 Repetition or iteration


DO WHILE logic tests for the terminal condition at the top of the loop. For example,




  WHILE condition DO

PERFORM block

ENDWHILE



Note the use of indentation and the way WHILE and ENDWHILE delimit the block. The REPEAT UNTIL structure tests for the terminal condition at the bottom of the loop:




  REPEAT

PERFORM block

UNTIL condition



Some analysts use a pseudocode structure much like a DO loop to define a count-controlled loop:




  DO index = initial TO limit

PERFORM block

ENDDO



Again, note the indentation and the ENDDO.



59.4.5 The case structure


A common programming problem involves selecting among several alternative paths. Although nested decision statements could be used to define such logic, the case structure is a better option when nesting goes beyond three or four levels.



The general form of a case structure is:




  SELECT variable

CASE (value-1) block-1
CASE (value-2) block-2
.
.
.
DEFAULT CASE block-n

ENDSELECT



The option selected by the case structure depends on the value of the control variable following the keyword SELECT. If the variable contains value-1, then block-1 is executed; if it contains value-2, then block-2 is executed, and so on. The DEFAULT CASE is coded to define the logic to be executed if the control variable contains none of the listed values. A case structure is delimited by ENDSELECT. Once again, indentation makes the structure easy to read.



For example, the following pseudocode routine accepts a code (the TRANSACTION TYPE) and, based on the value of the code, passes control to one of three lower-level routines.




  SELECT TRANSACTION TYPE

CASE (modify) PERFORM MODIFY STOCK
CASE (add) PERFORM ADD RECORD
CASE (delete) PERFORM DELETE RECORD
DEFAULT CASE PERFORM TRANSACTION ERROR
ENDSELECT



59.5 Key terms


Module —


A portion of a larger program that performs a specific task.


Procedure —


A set of guidelines, rules, and instructions for performing a task; often, a manual procedure.


Pseudocode —


A tool for planning, defining, or documenting the contents of a program routine or module that resembles real code.


Routine —


A set of instructions that performs a specific, limited task.



59.6 Software


Few software tools are designed to produce pseudocode. Word processors and text editors are sometimes used.



59.7 References


1.  Davis, W. S., Systems Analysis and Design: A Structured Approach, Addison-Wesley, Reading, MA, 1983.


2.  Gane, C. and Sarson, T., Structured Systems Analysis: Tools and Techniques, Prentice-Hall, Englewood Cliffs, NJ, 1979.


3.  Gillett, W. D. and Pollack, S. V., An Introduction to Engineered Software, Holt, Rinehart & Winston, NY, 1982.


4.  Peters, L. J., Software Design: Methods and Techniques, Yourdon Press, NY, 1981.

Comments

Popular posts from this blog

The Conversion Cycle:The Traditional Manufacturing Environment

The Revenue Cycle:Manual Systems

HIPO (hierarchy plus input-process-output)