A Worked Example
As an example, here is how a programmer would design and code a run length encoder using JSP.
A run length encoder is a program which takes as its input a stream of bytes. It outputs a stream of pairs consisting of a byte along with a count of the byte's consecutive occurrences. Run length encoders are often used for crudely compressing bitmaps.
With JSP, the first step is to describe the structure of a program's inputs. A run length encoder has only one input, a stream of bytes which can be viewed as zero or more runs. Each run consists of one or more bytes of the same value. This is represented by the following JSP diagram.
The run length encoder input
The second step is to describe the structure of the output. The run length encoder output can be described as zero or more pairs, each pair consisting of a byte and its count. In this example, the count will also be a byte.
The run length encoder output
The next step is to describe the correspondences between the operations in the input and output structures.
The correspondences between the run length encoders inputs and its outputs
It is at this stage that the astute programmer may encounter a structure clash, in which there is no obvious correspondence between the input and output structures. If a structure clash is found, it is usually resolved by splitting the program into two parts, using an intermediate data structure to provide a common structural framework with which the two program parts can communicate. The two programs parts are often implemented as processes or coroutines.
In this example, there is no structure clash, so the two structures can be merged to give the final program structure.
The run length encoder program structure
At this stage the program can be fleshed out by hanging various primitive operations off the elements of the structure. Primitives which suggest themselves are
- read a byte
- remember byte
- set counter to zero
- increment counter
- output remembered byte
- output counter
The iterations also have to be fleshed out. They need conditions added. Suitable conditions would be
- while there are more bytes
- while there are more bytes and this byte is the same as the run's first byte and the count will still fit in a byte
If we put all this together, we can convert the diagram and the primitive operations into C, maintaining a one-to-one correspondence between the code and the operations and structure of the program design diagram.
#includeRead more about this topic: Jackson Structured Programming
Famous quotes containing the word worked:
“... the next war will be a war in which people not armies will suffer, and our boasted, hard-earned civilization will do us no good. Cannot the women rise to this great opportunity and work now, and not have the double horror, if another war comes, of losing their loved ones, and knowing that they lifted no finger when they might have worked hard?”
—Eleanor Roosevelt (18841962)