Input-processing-output

Situation: 

Data needs to be transformed into other data, without user interaction after input.

Actions: 

Use abstraction to define each of the three parts, and the way they communicate. Abstraction means that you:

  • Name each subroutine.
  • Specify parameters and, for functions, return values.
Explanation: 

Input-process-output works best when there isn't much user interaction. This is common. Data might be in a file, or a database, a spreadsheet, whatevs. All users have to do is start the program, and maybe tell it where to get data from.

Examples:

  • Convert feet into meters
    • Get feet
    • Compute meters
    • Output meters
  • Compute the average of some data
    • Read the data
    • Compute total and count
    • Compute average
    • Show average

Use this pattern for an entire program, or part of one. For example, someone uses a program to take an online quiz. There's user interaction as the quiz is being taken, with data being stored in a database. After the quiz, the program uses input-processing-output to report quiz results to the user.

Programs with lots of controls (textboxes, option buttons, command buttons, etc.) are event-driven. They're less likely to use input-process-output for the entire program, although they might use it for some subtasks.

An example. Suppose you want to read data on sales transactions from a file, and output total sales.

Private Sub cmdStart_Click()
    Dim salesData As Variant 'Array
    Dim totalSales As Single
    'Input.
    salesData = inputSalesData
    'Process.
    totalSales = processSalesData(salesData)
    'Output.
    outputTotalSales totalSales
End Sub

The input step of Input-process-output is done by the function inputSalesData. It returns an array called salesData. The process step is done by the function processSalesData. It takes the parameter salesData, and returns one number: totalSales. The output step is done by the subroutine outputTotalSales. It takes the parameter totalSales.

Combine input and processing

A common variation is to combine input and processing into a single step. Like this:

Private Sub cmdStart_Click()
    Dim totalSales As Single
    'Input-process.
    totalSales = inputProcessSalesData
    'Output.
    outputTotalSales totalSales
End Sub

inputProcessSalesData might be like this:

Function inputProcessSalesData() as Single
    Dim totalSales as Single
    'Open the file.
    'For each record in the file:
        'Read one sales record.
        Line Input #1, sales
        'Accumulate in total.
        totalSales = totalSales + sales
    '...
    inputProcessSalesData = totalSales
End Function

The program doesn't keep an array of sales data in memory. It isn't needed to compute the total. The program is simpler and faster.

This won't work for all tasks. For example, if you want to compute the median, you need to sort the data and grab the value in the middle. You'll need to keep the data in memory so you can sort it. (That's one way to do it, anyway.)