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.
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.
'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:
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.
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.
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…
The chunks need to work together. Here's the tip program again.
amountis how those two connect together.
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:
Here's the official summary of thunking. You can tell it's official, because it has a badge.
- Specify the goal.
- Decide on an overall pattern for a program that meets the goal.
- For each chunk in the pattern, decide how it will connect to other chunks.
- 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.
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.