Decomposing a Problem into Subprograms
Splitting a task into parts small enough to write and check one at a time, then wiring the parts together into the whole program.
What a learner can do afterwards
- Break a described program into three or four named jobs before writing any code
- Write each part as its own function and test it on its own
- State what each part takes in and what it gives back
1 · Read
Split the task into a few named jobs before you write any code. To cross a street you would check for cars, decide if it is safe, walk across, then check again. Each job is small enough to hold in your head.
Rock, paper, scissors splits into get each choice, compare them, decide the winner, then announce it. decide_winner takes both picks in and gives the winner back. You can test it alone with made-up picks.
State what each part takes in and what it gives back. get_choice takes nothing in and gives a pick back, while announce_result takes the winner in and only prints. Clear ins and outs let the parts plug together.
Build and check each small piece on its own, then wire them into the whole. Small tested pieces make a program you can trust.
Name the small jobs, fix what each takes and gives, test each alone, then wire them together.
2 · Watch
Take it off screen
Where it sits
8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.