# Chunks and patterns

Here's Diana.

Diana's brain can only think about a few things at the same time. All human brains are like that. If she tries to think about too many things at the same time, she won't be happy.

My brain hurts!

So how can Diana do complicated tasks? They key is what we said earlier.

Diana's brain can only think about a few things at the same time.

Thunking is short for thinking-in-chunks. It's how brains do tasks that are too complex to fit in brains all at once.

• Break up the task into chunks.
• Work on each chunk separately.
• Put the chunks together.

Working on task pieces separately means that the brain is never overloaded.

Let's see how you do it. But first, before you do anything else…

# Define the goal

You work out what the end product will be. That's the very first thing. If the assignment is one thing, and you do another, you'll have wasted your time. That's a Bad Thing.

Misunderstanding business goals is the number one reason programming projects fail.

So, how to define the goal. Take the Aussie rules program. You might start by drawing out the spreadsheet. What it looks like before the programs runs, and after:

The drawing shows what the program is supposed to do.

In RL (real life), you often don't know exactly what your program will be doing. You need to allow for that uncertainty. For this intro course, we'll assume that we know outcomes before we start.

# Choose the big chunks

After choosing the outcome, you decide on the big chunks. But how? In sewing, there are patterns that show you the chunks. Sleeves, cuffs, etc.

There are patterns in programming, too. In programming, a pattern is a common way to do a task, that people find useful. You've already seen a pattern. Here's the tip code.

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
Here's the Aussie rules code:

1. 'Declare variables
2. Dim goals As Integer
3. Dim behinds As Integer
4. Dim score As Integer
5. 'Get data from the worksheet
6. goals = Cells(1, 2)
7. behinds = Cells(2, 2)
8. 'Compute results
9. score = goals * 6 + behinds
10. 'Put results into worksheet
11. Cells(3, 2) = score
Here's the animal sound code:

1. 'Declare variables
2. Dim animal as String
3. Dim sound as String
4. 'Get data from worksheet
5. animal = Cells(1, 2)
6. 'Compute sound
7. If animal = "dog" Then
8.     sound = "woof"
9. ElseIf animal = "cat" Then
10.     sound = "meow"
11. Else
12.     sound = "Don't know"
13. EndIf
14. 'Output to worksheet
15. Cells(2, 2) = sound
The comments show the pattern chunks:

'Declare variables
'Get data from worksheet
'Compute
'Output to worksheet

This is the most common pattern for programs. It's called Input-Processing-Output (IPO). Each chunk is a group of statements. Put the chunks together, and you have a program.

You'll seeing other patterns in the course. They're documented like this:

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.

Each pattern has:

• A name
• Situation where it applies
• How to use it

What you see above is a summary of the pattern. See the More… link? Open it in a new tab (middle-click the link, CTRL+click the link, or right-click the link and choose New tab or New window). You'll see the full details.

Lily
The VBA programs all have "Declare variables" as a first step, but that isn't in the pattern. Why is that?

Tara
Good question!

The patterns are "language agnostic," that is, they can be used with any programming language. In VBA, you have to declare variables before you use them when Option Explicit is present (which we always do). However, in PHP, Python, and other languages, you can't declare variables in the same way.

That's why "Declare variables" isn't in the pattern: it doesn't apply in some common languages.

Klaus
So, if I take a PHP course, I can still use the patterns from this course?
Tara
Yes! You can use the input-processing-output (IPO) pattern in PHP, JavaScript, Python, Ruby, C++, Java, whatevs.

## The pattern catalog

When you're working on an exercise, in this course or another, it can help to have a list of patterns. You can quickly scan the list, and see if there are patterns that apply to the task you're working on.

That's what the pattern catalog is for. There's a link to it in the Tools menu in the right-hand toolbar. You'll find links to where each pattern is used in the course, a keywords to find related patterns.

Back to Diana and Ben…

# Connecting chunks

The chunks need to work together. Here's the tip program again.

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
Where are the connections between the chunks?

Simon
Is it the variables? Like, "Get data from worksheet" and "Compute results." The variable amount is how those two connect together.
Tara
Good thinking, Simon!

Yes, it's the variables that connect the chunks together.

Look at this code:

'Get data from the worksheet
amount = Cells(1, 2)
'Compute results
tip = mealAmount * 0.15

That wouldn't run. The chunk "Get data from the worksheet" puts the cost of the meal in the variable amount. The program chunk "Compute results" expects the cost of the meal in the variable mealAmount. The two chunks aren't connected right.

If one human was writing the input chunk, and another human was writing the processing chunk, they should get together before they start coding, and agree on how the chunks connect. In this case, they would agree on a variable.

# Patterns aren't Legos

With Legos, the bricks are fixed. You can't change them. You connect them together; that's the whole point. But you don't start carving pieces off one, and gluing them onto another.

Patterns aren't like that. They're starting points. You melt them to change their shape. You blend them together. Until you get what you need. Even if it's this:

# Thunking

Here's the official summary of thunking. You can tell it's official, because it has a badge.

1. Specify the goal.
2. Decide on an overall pattern for a program that meets the goal.
3. For each chunk in the pattern, decide how it will connect to other chunks.
4. If a chunk is too big to think about all at once, apply these same steps to that chunk.

Big chunks are broken down into smaller ones. Small chunks are broken down into even smaller chunks, and so on. Until the tiny chunks fit into your head. If you've done the connections right, the chunks should all fit back together to make the entire program.

# Summary

Before you write a program, be sure to understand the goal. Drawing input and output on paper can help.

Humans thunk when they design things. They divide the task into chunks, so they can think about one chunk at a time. Before they start working on a chunk, they should decide how the chunks will fit together.

Identify the big chunks for a task. Look for existing patterns you can use. A pattern is a way of doing something that has proved successful in the past.

This Web site has a pattern catalog. Use it if you need ideas about how to tackle a task.