Learning outcomes
By the end of this lesson you should be able to: take a problem and break it into smaller functions; decide what each function takes in and hands back; assemble those functions into a working whole; and recognise the difference between writing a program and designing one.
Prerequisites
Lessons 7, 8 and 9. You need functions and the data structures, and the capstone reading pattern — because decomposition is the skill of taking a program like that and organising it into named pieces you can reason about one at a time.
The big idea
This is the skill that separates someone who can write code from someone who can build software. A short program can be written as one long list, but a real program cannot — it would be a wall of text you can hardly read, and any change would ripple through it unpredictably. Decomposition is how you make a big program tractable: divide it so that you can understand, test, and fix one piece at a time.
Why this matters before the technique
The drive behind decomposition is simple: it is easier to think about small things than large ones. Each function should do one job and do it well. If you catch yourself saying a function "does a few things", that is the signal to split it — the moment a function's job needs an 'and' in its description, it probably should be two functions.
Deciding the interface is the hard part. Before you write a function's body, decide what it needs as input and what it produces as output. If a function is meant to compute a grade from a score, its input is a score and its output is a grade. If it is meant to produce a sentence, its inputs are a name and a score, and its output is a string. Nailing down the parameters and the return type first is what makes the pieces fit together without surprises.
There is an art to choosing the boundaries. A good decomposition has functions that are reusable and readable on their own. A bad one has functions that are too large, too small, or that depend on each other's internals. The test is usually this: can you read a function's name and what it returns and understand its job without reading its body? If yes, you have a clean boundary; if no, the piece is doing too much.
Worked example 1: a problem split into three small functions
A trivial greeting, but decomposed. Each function has one job, and a tiny bit of code assembles them.
def get_name():
return "Ada"
def make_greeting(name):
return "Hello, " + name + "!"
def print_greeting(greeting):
print(greeting)
print_greeting(make_greeting(get_name()))Hello, Ada!Read the pieces separately. get_name produces a string. make_greeting takes a string and produces a longer string. print_greeting takes a string and shows it. No function knows how the others work — each just receives a value and hands one back. The final line composes them: get_name() is fed into make_greeting, whose result is fed into print_greeting. This is the whole idea: each piece is simple and testable on its own, and the whole program is just onesimplification of functions.
Worked example 2: a slightly more realistic decomposition
Grade a student using two functions. One computes a letter grade from a number; the other builds a readable summary. The summarising function calls the grading one.
def letter_grade(score):
if score >= 85:
return "A"
elif score >= 70:
return "B"
return "C"
def summarize(name, score):
return name + ": " + letter_grade(score)
print(summarize("Ada", 92))
print(summarize("Ben", 74))Ada: A
Ben: BNotice how the two functions divide the work. letter_grade is concerned only with the mapping from a number to a grade — it is the decision logic from lesson 4, wrapped up. summarize is concerned only with arranging a name and a grade into a readable sentence. Neither cares about the other's details. If the grading threshold changes, you edit letter_grade alone and summarize needs no change; if you want to show the grade differently, you edit summarize. Each piece is independent, which is exactly what makes the program understandable and maintainable.
Common mistakes
Watch outMaking one giant function. If a function's job needs an 'and' — 'it validates and formats and saves' — it is likely too big. Split it so each piece does one thing.
Watch outNot deciding inputs and outputs first. Writing a body before you know what it takes in and gives back leads to functions that don't compose. Decide the interface first.
Watch outOver-decomposing into tiny meaningless functions. A function that wraps a single print with a misleading name adds noise rather than clarity. Split where it clarifies, not for its own sake.
Watch outDuplicating logic instead of extracting a function. If the same calculation appears in two places, make it one function and call it from both — that is the reuse payoff of decomposition.
Check your understanding
What is the main benefit of decomposition?
What decision should you make before writing a function's body?
How can you tell when a function is too big?
Multiple choice
What is decomposition?
Which is the best first step after deciding what a problem needs to do?
What is the signal that a function is too big?
Why is 'reuse' a benefit of decomposition?
What is the trade-off to avoid when decomposing?
In a decomposed program, how should functions relate to one another?
Which change is easiest to make if the grading thresholds change?
How does decomposition relate to the word-count program?
Summary and next
Decomposition is breaking a problem into one-job functions with clear inputs and outputs. Decide the interface before the body; keep each function doing one thing; and compose them so the whole program is a chain of understandable parts. This is what turns a wall of code into a design.
Next: testing & debugging. Lesson 10 organised your code into pieces; the next lesson shows you how to test each piece and find the bug when one of them misbehaves — moving from the debug technique of lesson 6 to a disciplined, build-your-own-tests approach.