Patterns

Tags

Log in

If you're a student, please log in, so you can get the most from this page.

Fill in the blank

What is it?

In programming, a _________ is a piece of memory, with a name.

Your answer:
Saving
Fill in the blank

Fixed value

A ______________ is a fixed value, like 3.14, 0.41, or 512.

Your answer:
Saving

What's a pattern?

A pattern is a way of doing something, or describing something, that helps people get stuff done. Here are patterns that different people use.

  • Chef: Frying. Braising. Caramelization. Egg wash. More...
  • Dancer: Box step. Heel turn. Outside partner step. More...
  • Skate boarder: Big spin. Kick flip. Handstand flip. More...
  • Carpenter: Butt joint. Dowel joint. Dovetail joint. More...

What to be a chef? You have to learn what braising is, and how to do it. That means practicing. Braise this, braise that, until you get good at it.

When you talk to other chefs, you can mention braising, and they'll all know what you mean. It's part of chef-speak.

Same with the other skills. Hard to imagine a carpenter who doesn't know what a dowel joint is.

Every human skill has patterns.

Reflect

Think about a skill you're good at. Sewing, curling, gaming, milking goats, whatevs.

Name the skill you're good at, and list a few patterns that people with that skill have in their heads.

(If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site.)

When someone learns patterns, tasks become easier. Suppose the task is: "Here's some sales data. Are sales in the west higher or lower than sales in the east?" If you know the statistical t-test pattern, you know how to do the task. If you've practiced that pattern, you can zip through the task, and get back to cooking, dancing, milking goats, or whatevs.

Goat

Warm your hands, please.

Programming is all about patterns

There are patterns in programming, too. Learning a few of the basic ones makes programming much easier.

One of the basic patterns is IPO, for input-processing-output.

Pattern

Input-processing-output

A program inputs data from somewhere, does computations with the data, and outputs the result.

Both versions of the tip program from the last two lessons use IPO. One version did input/output (IO) from worksheet cells.

Tip worksheet

The program used the IPO pattern:

  • Get the meal price from cell B1 (input)
  • Compute the tip and total (processing)
  • Put the tip in cell B5, and the total in B6 (output)

The other version did I/O with dialogs.

Tip dialogs

The program used the IPO pattern as well:

  • Get the meal price from a dialog (input)
  • Compute the tip and total (processing)
  • Show the results in dialogs (output)

The IPO pattern is:

  • Input
  • Processing
  • Output

The pattern doesn't say where the input comes from, or what variables the data goes into. It doesn't say what the processing is, or where the output goes. When you use the pattern for a particular task, you decide on the input, processing, and output for that particular task.

Dollars to euros

Let's look at another example. Say you have a new task: write a program to convert US dollars to euros. We'll use a simple fixed conversion rate. Euros = dollars * 0.9.

The user types in a dollar amount, clicks a button, and gets the equivalent in euros. The program will be IPO:

  • Input: get dollars from somewhere
  • Processing: compute euros
  • Output: show euros somewhere

There are three pieces, I, P, and O. Let's give each one to a different person:

  • Input - Adela
  • Processing - Georgina
  • Output - Marcus

Here's the program, in pseudocode. Since your computer is stupid, we need to tell it to use variables to keep track of everything.

  • Get a value from somewhere, and put it into a dollars variable (Adela).
  • Multiply the dollars variable by 0.9, and put the result into a euros variable (Georgina).
  • Put the euros variable somewhere (Marcus).

Let's put the three programmers into different rooms, and have them each write their part of the program. Adela, you go into the Blue Room. Georgina, into the Bit Pit. Marcus, into the Red Room.

Adela does input

Adela, what's the input code?

Adela
Adela

You need to tell me where the input will come from. Worksheet cell, or dialog?

Oh, right. We have two input patterns that would work.

Pattern

Input from a cell

Get input from a cell.

Pattern

Input from a dialog

Use a dialog to get some input.

Adela doesn't know which one to use. Usually, in a business, someone will tell you what they want. The list of what they want is a list of requirements. The programmer's job is to meet the requirements.

Let's say I/O is supposed to come from a worksheet.

Currency converter

Adela
Adela

OK. Here's my code.

  • Dim sDollars As Single
  •  
  • 'Input dollars from cell.
  • sDollars = Cells(1, 2)

That's the first bit done:

  • Input - Adela DONE
  • Processing - Georgina
  • Output - Marcus

(BTW, you don't have to do the steps in order. More later.)

Georgina does processing

What's the code, Georgina?

Georgina
Georgina

This.

  •   'Processing.
  •   sEurosAmount = sDollarsAmount * 0.9

Thanks. That's the second bit done.

  • Input - Adela
  • Processing - Georgina DONE
  • Output - Marcus

Marcus does output

Hey, Marcus, you're up.

Marcus
Marcus

On it! The code would be... um, where does the output go?

Oh, right. There are two output patterns that would work.

Pattern

Output to a cell

Copy a value to a cell in a worksheet.

Pattern

Output to a dialog

Use a MsgBox to show a dialog box.

Let's put the output in the worksheet, in cell B5, like this:

Currency converter

Marcus
Marcus

Here's the code.

  •   'Output.
  •   Cells(5, 2) = sAnswer

Great! All done.

Put the code together

Here's the program, all three parts stuck together.

  • Dim sDollars As Single
  •  
  • 'Input dollars from cell.
  • sDollars = Cells(1, 2)
  • 'Processing.
  • sEurosAmount = sDollarsAmount * 0.9
  • 'Output.
  • Cells(5, 2) = sAnswer
Reflect

How does the code look to you?

(If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site.)
Ray
Ray

Umm... dude. The variable names are all different.

I'm guessing computers are too stupid to know what you mean.

Doh! You're right.

  • Dim sDollars As Single
  •  
  • 'Input dollars from cell.
  • sDollars = Cells(1, 2)
  • 'Processing.
  • sEurosAmount = sDollarsAmount * 0.9
  • 'Output.
  • Cells(5, 2) = sAnswer

A human would know that sDollars and sDollarsAmount are the same thing. A computer won't figure that out. It will do exactly what you tell it to, and never what you want it to.

Truth Goat

Truth Goat: Computers never know what you mean, only what you say.

True dat, TG.

That's the source of most bugs for a beginner: computers don't know what you mean. You have to get used to being very precise, because computers are very very very very very stupid.

What went wrong?

When you write programs, your main tasks are:

  • Understand what the requirements are.
  • Choose patterns.
  • Make your pattern implementations work together.

We got the first two right, but not the last one.

In IPO, the input chunk passes data to the processing chunk.

The input chunk passes data to the processing chunk

​The input chunk passes data to the processing chunk

It's variables that pass data from one chunk to the other. The input code puts data into variables. The output code pulls data from those variables.

Variables pass data from one chunk to the other

​Variables pass data from one chunk to the other

In our code, input put data into one variable, and processing got data from a different variable.

Different variables

​Different variables

That won't work. The chunks need to use the same variable to pass data around.

Use the same variable to pass data around

​Use the same variable to pass data around

Choose a different variable name, if you want. This would work just as well:

Using sDollars rather than sDollarAmount

​Using sDollars rather than sDollarAmount

Choose a name that makes it easy for you to understand the code.

Using patterns

You'll learn a bunch of patterns in this course. You can see them in the pattern catalog. You can get to it from the Lists item in the main menu. (Remember the right-click-open-in-new-tab trick.)

Showing the pattern catalog

Most of the patterns have code that you can copy-and-paste into your own programs.

Ray
Ray

Copying code? Isn't that cheating?

No, not if it's code from this website, or another of your own programs.

It's not that easy though. Copying and pasting won't work unless you understand the patterns. If you don't understand the patterns, you won't know which ones to use, how to make them fit your program's requirements, and how to make them stick together.

Time for an exercise.

Exercises

Use IPO for this one.

Exercise

Aussie rules

Australian Rules is a type of football. You score points by getting the ball between posts at either end of the field:

Aussie rules

If the ball goes between the center two posts, that's a goal, and is worth six points. If the ball goes between one of the center and outside posts, that's a behind, and is worth one point.

Make a worksheet that starts like this:

Start

The user enters values for goals and behinds, clicks the button, and your VBA program outputs the total score. Here's an example:

Output

Here's another example:

Another example

Upload your workbook.

Exercise

Aussie rules with dialogs

Earlier, you wrote a program to compute an Aussie rules score. Do it again, but with dialogs for input and output.

The workbook start like this:

Start

The user clicks Run, and is asked for the number of goals. Let's say they type this:

Input number of goals

They click OK, and are asked for the number of behinds. Suppose they type:

Input number of behinds

They click OK, and see the output:

Output

W00f!

Upload your workbook.

Next up

More about computer arithmetic.