Code Bytes · "Start Here: What Is Coding?" · Episode 7 · Thinking like a coder
Breaking problems into steps
Coders tackle a problem that feels too big by splitting it into smaller problems, then splitting those again, until every piece is something they can code. Then they solve the pieces one at a time. A to-do list app splits into show, add, tick and save, and "add a task" splits into three even smaller steps.
Too big to code in one go
Imagine someone asks you to "build a to-do list app". Where do you even start? Nobody writes a whole app in one go, and staring at the big goal is how people get stuck.
Coders don't try to solve the big problem directly. They break it into smaller problems, and then break those into even smaller ones.
Split it
A to-do list app can be split into four smaller jobs:
`` To-do app ├── show the list ├── add a task ├── tick a task off └── save the list ``
Each of these is easier to think about than "the whole app". (This is one reasonable split, not the only one: a real app would add more pieces, like deleting or editing tasks.)
Still too big? Split again
"Add a task" is still a bit big, so split it again:
`` add a task ├── read what the person typed ├── put it at the end of the list └── show the list again ``
Now each piece is small enough that you can picture the code for it.
Keep going, then solve one at a time
The rule is simple: keep splitting until every piece is something you can code. Then solve the pieces one at a time. When all the small pieces of "add a task" work, "add a task" works. When all four jobs work, the app works.
Small pieces have another bonus: you can check each one on its own, so when something breaks, you know where to look.
Try it yourself
Here are the three small steps of "add a task", each as one line of Python. Open any free online Python editor (search "online Python") and run:
``python tasks = ["Buy milk", "Walk the dog"] typed = "Water plants" # step 1: read what the person typed tasks.append(typed) # step 2: put it at the end of the list print(tasks) # step 3: show the list again ``
You should see:
`` ['Buy milk', 'Walk the dog', 'Water plants'] ``
Don't worry about the details yet (lists get their own episode later). Notice how each small step became one small line. Now try the same split on paper for "tick a task off": what are its two or three smaller steps?
Good to know
- No code is run in the video. The snippet above was run with Python 3 and printed the output shown.
- Breaking problems down is one of the four ideas usually taught as "computational thinking", together with pattern recognition, abstraction and algorithms (as in UK school computing courses such as BBC Bitesize and Barefoot Computing).
- The to-do split in the video is one reasonable breakdown. Real apps have more pieces, like deleting tasks, editing them or syncing between devices.
- Small pieces are also easier to test on their own. Testing small pieces of code (unit testing) is a topic for a later course.
Answer to the comment question
What's the coder's word for breaking a big problem into small ones? Decomposition. To decompose something is to break it into its parts. In coding, decomposition means splitting a big problem into smaller problems, and those into even smaller ones, until each piece is simple enough to solve.
Test your knowledge
Quick quiz: Breaking problems into steps
4 multiple-choice questions. Pick one answer for each, then check how you did.
Sources
- Barefoot Computing, "Quick guide to computational thinking": breaking problems into smaller parts
- BBC Bitesize, "KS3 Computer Science: computational thinking"
- Seneca Learning, "IGCSE Computer Science revision notes: computational thinking"
Video transcript
How do coders tackle a problem that feels way too big? They break it into smaller problems. Then they break those into even smaller ones.
Take a to-do list app. Too big to code in one go. So split it: show the list, add a task, tick a task off, save the list.
Still too big? Split again. Adding a task means: read what the person typed, put it at the end of the list, then show the list again.
Keep going until every piece is something you can code. Then solve them one at a time.
Coders call this decomposition. Big problems are just lots of small ones.
What's the coder's word for breaking a big problem into small ones? Comment your answer below!
That's your byte.
More from "Start Here: What Is Coding?"
- "Start Here: What Is Coding?" EP2What a programming language is
- "Start Here: What Is Coding?" EP3Apps, websites and programs
- "Start Here: What Is Coding?" EP4Do you need math to code?
- "Start Here: What Is Coding?" EP5What software engineers actually do