Patterns nest

IPO rides again

Input-processing-output (IPO) is the most common pattern. Here it is in action:

  1. 'Declare variables
  2. Dim amount As Single
  3. Dim tip As Single
  4. Dim total As Single
  5. 'Get data from the worksheet
  6. amount = Cells(1, 2)
  7. 'Compute results
  8. tip = amount * 0.15
  9. total = amount + tip
  10. 'Put results into worksheet
  11. Cells(2, 2) = tip
  12. Cells(3, 2) = total
There are three chunks here, forgetting declaring variables for now. In this and the other programs you’ve seen, input is always the same: from the worksheet. But input can come from other places. In fact, input from the worksheet is itself a pattern.

Pattern

Input from worksheet cells – known amount of data

Situation:
Your program needs input from the user. You know exactly how many elements you need, and what type each one is. The tip program is an example.
Actions:
Choose a few cells on a worksheet. Tell users what data to put in the cells.

Input patterns

Here are some other input patterns.

Input from the keyboard

You can ask the user to type something at the keyboard. Look at line 6 in this example:

  1. 'Declare variables
  2. Dim amount As Single
  3. Dim tip As Single
  4. Dim total As Single
  5. 'Get data from the keyboard
  6. amount = InputBox("How much was the bill?")
  7. 'Compute results
  8. tip = amount * 0.15
  9. total = amount + tip
  10. 'Put results into worksheet
  11. Cells(2, 2) = tip
  12. Cells(3, 2) = total
Pattern

Input from keyboard – known amount of data

Situation:
Your program needs input from the user. You know exactly how many elements you need, and what type each one is. The tip program is an example.

Actions:
Use the InputBox function.

Input from a file

Maybe you have a file that was downloaded from the Internet, with the data you need. You can input data from it.

Pattern

Input from a file – known amount of data

Situation:
Your program needs input from the user. You know exactly how many elements you need, and what type each one is. The data can be obtained in a file.

Actions:
Use the File open, process, close pattern to open and close the file. Use the Input statement to read data.

More on files later in the course.

Input is abstract

Here’s IPO again.

Pattern

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.

​ The “input” step doesn’t tell you exactly ​how to code something. It’s just: Get input from somewhere, dude.

Input is an abstract step. To implement it, you need to use a concrete pattern like “Input from worksheet cells,” “Input from keyboard,” or “Input from file.” These concrete patterns nest inside IPO, which is completely abstract.

Output patterns

As for input, so for output. The O in IPO is abstract. You need to pick a concrete pattern when you write actual code.

Output to a worksheet cell

You know this one.

Pattern

Output to worksheet cells – known amount of data

Situation:
Your program needs to output data to the user. You know exactly how many elements you want to display, and what type they are. The tip program is an example.
Actions:
Choose a few cells on a worksheet. Output data to those cells with the Cells method.

Output to the screen

Pattern

Output to screen – known amount of data

Situation:
Your program needs to output data to the user. You know exactly how many elements you want to display, and what type they are. The tip program is an example.

Actions:
Use MsgBox.

Output to a file, maybe a Web page

Pattern

Output to a file – known amount of data

Situation:
Your program needs to output data. You know exactly how many elements, and what type each one is.

Actions:
Use the File open, process, close pattern to open and close the file. Processing uses the Write or Print statements.

Later, you’ll learn how to write a program that creates a Web page.

It’s all good

A benefit of thunking is that you can switch out the patterns you use. Does your program need to get input from a different place? Change the I, and leave P and O alone.

Exercises

Exercise: Compute tip again
Redo the compute tip program, so that it gets input from a dialog box, and shows output in a dialog box..

Upload your Excel file.

(If you were logged in as a student, you could submit an exercise solution, and get some feedback.)

Exercise: Favorite animal again
Redo the favorite animal program with different I/O. That’s geek for input/output. Get data from the keyboard, and show output to the screen.

Also, add two more animals.

Upload your Excel file.

(If you were logged in as a student, you could submit an exercise solution, and get some feedback.)

Summary

Input in IPO is an abstract step. To implement it, you need to use a concrete pattern like “Input from worksheet cells,” “Input from keyboard,” or “Input from file.” These concrete patterns nest inside IPO.

The O in IPO is abstract as well. You need to pick a concrete pattern when you write actual code.

A benefit of thunking is that you can switch out the patterns you use. Just change the code you need to, and leave the rest alone.