Read-Process-Write Pattern: Overall Structure of Program Components

Pattern Name:

Read-Process-Write Pattern: Overall Structure of Program Components


Viera K. Proulx

Intent and Motivation:

A program is a description of a process that transforms the initial data into a new form that we call the result. The initial data (input) can come in many different forms (text, picture, sound, voice, file that represents sound or picture or text, a mouse location and mouse click, etc.) The output again can come in many forms - a new window opened on the screen, a picture, a control signal to car brakes, as well as traditional text.

Every code segment typically implements one simple task. To do the task well, first consider carefully what kind of information is needed to complete the task. Then decide how this information will be supplied. Next, list all information that will be generated by this code segment and decide how is it going to be communicated to the user of the code segment. Finally, design the processing part of the code.

Problem Examples:

  1. A program reads a MIDI file and plays the music.
  2. A function returns true if the first argument is smaller than the second and returns false otherwise.
  3. A program reads ten numbers and computes the sum and average.

Problem and Context:

Every time you design a code segment, start by writing down all the information the program will need. Divide the information items into three parts. The first part is data that will be used by the code but will not be modified in any way. The second part is data that will be generated by this code. The third part is data that this code will use and modify. That means, that some initial values will be supplied when the code segment starts executing, but these will be modified and made available in the modified form when the code segment completes its task.

The first part is then further divided into two. Some of the data values that will be needed are known ahead of the time (value of pi, number of inches in a foot, etc.) others will vary each time the code segment is run. The known quantities will become constants, the other data will be the input.

Data generated by the code segment may not all be needed by the user of the code segment. If a code segment computes the average of the list of numbers, the user may not be interested in how many items were processed, only in the computed average. So, the next decision is what data will become output and what form will it take.

The third set of data is the hardest to deal with. It plays the role of input and output at the same time. That means that the user must supply some variables or objects for the code segment to work with, and the code segment will be able to modify the information stored in these variables or objects. The code segment and the user in effect share some variables or objects, though the user is responsible for declaring them, defining the initial values, and finally disposing of the storage area used by these variables and objects.

When thinking about the process that will perform the desired task, think big! Start with understanding what needs to be done before you write any code. Then think about the constructs (statements) you will need to accomplish the task. Try to match the problem to one of the known patterns. If the task seems too complex, divide it into smaller parts. If there is some task that seems to divert the attention from the main problem, separate that task into a helper function or other independent code segment. For the purpose of the problem at hand, assume you know how to solve the helper task and work on that task later.

Finally, assemble all pieces into a coherent code and document your work.

Required Elements and Structure:

This is a design pattern rather than programming pattern. Every problem will be different. You will need to use the appropriate Name Use Patterns to declare and define the identifiers that will be needed, the Read Data Pattern to process user input, and many other patterns to complete the rest of the task.

The reason this pattern is included is to give you the guidance on what needs to be considered before any part of a program is written. It also makes you realize that understanding what information a code segment works with, how is this information transformed, and what is the final result and effect is at the core of program design.

The key elements in designing any segment of code are the following:

It is a good practice to add comments to every significant code segment that specify these three points. For example, a function that converts hours and minutes to minutes may assume that hours are given as a positive integer and minutes are in the range between 0 and 59. Side effects are the results of the actions of the code segment that do not seem to affect the caller, but change the overall working environment. For example, if a function paints a rectangle, the user variables are not changed, but the appearance of the graphics window has been altered.

Implementation Examples:

Instead of an example we will show a small case study. You are to write a program that will display a list of values as a bar chart on the screen. What information do you need to be able to do this? Of course you will need the data items. You will also need to be told (or will have to find out as data is entered) how many items you will display. You will need to know the size of the display area. You will need to find out the magnitude of the largest item, so you can decide on the scale. You may want to know the color in which to display the chart. Your program will generate the display - that is the output. What are the inputs? Well, the size of the display may be stored in some existing variable, or may be part of the actual input. The data items may be typed in by the user, read from a file, or may be stored in the computer already. You will need a variable to store the number of items in your list, but this does not need to be explicit input. You will probably need to compute the maximum value among the data items - that is a task that should be delegated to another code segment. You will need to figure out the scaling factor and spacing of the bars. This is another task to be performed by a helper code segment. You are now ready to design the program. Start by reading the data. Count the items, unless it has been done already. Find the maximum value. Figure out the scaling factor and spacing. Figure out the color of the chart and set the color. For each data item, display the item with proper scaling and spacing. Write a message about completion of the task.


Every code segment can be divided into three logical parts: Read the input and set up the initial values of constants and data needed for the processing. Perform the processing step, generating new data. Communicate the result data to the code segment user.

The design strategy is to identify all data that will be needed, all data that may be modified, and all data that will be generated. Decide how each data item will be represented, initialized, and manipulated. Decide how the output will be communicated to the user. Design the main processing part of the code segment, keeping in mind that any complex tasks should be performed by separate helper code segment.