Too big to build all at once
How do you build a whole shopping website? It is too big to build all at once.
But a shop has a few clear jobs. Show the products. Hold a basket. Take a payment. Send an email. You can build each job on its own.
Key words
| Word | What it means | Example |
|---|---|---|
| Decomposition | Breaking a big problem into smaller parts. | a shop: basket, payment, email |
| Feature | One thing the system must do or have. | search for a product |
| Block diagram | Boxes that show the parts of a system. | shop, basket, payment |
| Information flow diagram | A picture that shows what data moves between the parts. | order goes to the shop store room |
| Flowchart | A picture of steps, with arrows, in order. | start, check stock, end |
| Function | A named piece of code that does one job. | def item_cost |
The cells students fill in are left blank in this preview.
Warm up: break it down
Have a go
Warm up: break it down
3 questionsWhy break a problem down?
A small part is easier to understand. It is easier to build, and easier to test.
A team can share the parts. One person does the basket. Another does the payment. You can also use a part again in another project.
There is a cost too. The parts must fit together later. Too many tiny parts are hard to look after.
The four tasks of decomposition
| Task | What you do | Cinema booking example |
|---|---|---|
| 1 Find the main features | List what the system must do. | Show films, pick seats, pay, send ticket |
| 2 Describe each feature | Say what goes in, what comes out, and what it does. | Pick seats: in = film and time. Out = chosen seats. |
| 3 Break the problem down | Split a feature that is still big. | Pay: check card, take money, show receipt |
| 4 Break the solution down | Split the plan or the code into small parts. | One function for each small job |
The cells students fill in are left blank in this preview.
Which task is it?
Have a go
Which task is it?
3 questionsMethod 1: a block diagram
Method 2: an information flow diagram
A flow diagram shows the data
In a block diagram the top box is the whole problem and each box below is one part. It shows the parts. An information flow diagram shows the data that moves between the parts.
Every arrow has a label. The label says what travels along it.
Your turn: finish the flow diagram
A library lets students borrow books. The diagram has three gaps marked ???.
- Replace each ??? with the data that moves along the arrow.
- Add one more part, Send reminder.
- Join it to the other parts with an arrow that has a label.
flowchart LR
student([Student]) -->|book name| search[Find the book]
search -->|??? | loan[Lend the book]
student -->|library card number| loan
loan -->|??? | record[Save the loan]
record -->|??? | student
Make a free account to save your work, or press Start lab to work through the whole lesson.
Method 3: a flowchart
Method 4: code
def pick_seats():
return "A1, A2"
def pay(seats):
return "Paid for " + seats
seats = pick_seats()
print(pay(seats))
# Add a function send_ticket and call it.
Make a free account to save your work and track your progress.
Method 5: a written description
You can also write the parts in plain words.
- The customer picks a film and seats.
- The system shows the price.
- The customer pays.
- The system emails the ticket.
Writing is quick. But a long text is hard to read quickly.
Match the method to its strength
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Watch: break it down
Press Play on the short film below. It breaks the cinema booking system down with the four tasks, one at a time, into a tree of parts and then into code. Watch how a feature that is still big is split again, and why a list of steps is not the same thing.
Film: decomposition, step by step
Decomposition: break it down
Every scene, as text
- Decomposition means breaking a big problem into smaller parts. The whole problem, a cinema booking system, is one box at the top: too big to build all at once. The empty boxes under it are the parts still to find.
- Task 1, find the main features: list what the system must do. Show films, Pick seats, Pay and Send ticket each go in a box under the whole problem. Boxes that show the parts make a block diagram.
- Task 2, describe each feature: say what goes in, what comes out and what it does. Pick seats takes in the film and time, the customer picks the seats, and out come the chosen seats.
- Task 3, break the problem down: split a feature that is still big. Pay becomes Check card, Take money and Show receipt. A small part is easier to build and to test, and two people can build two parts at the same time.
- Task 4, break the solution down: one function for each small job. pick_seats() gives back "A1, A2", pay(seats) takes those seats, and print(pay(seats)) prints Paid for A1, A2. The parts must fit together.
- Myth (false): decomposition means steps in order. Start, Pick seats, Seats free?, then yes to Pay and End is Method 3's flowchart: it shows the order things happen. Decomposition shows which parts there are. Both are useful, but only one is decomposition.
Your turn: three small functions
A small shop makes a receipt. Break the job into three small functions.
- item_cost gives price times quantity.
- delivery_charge gives 0 for 20 pounds or more, and 3 for less.
- receipt_line gives text like 2 x Cola = 3.0.
Press Run Tests when you finish each one.
def item_cost(price, quantity):
# Cost of one kind of item: price for one, times how many.
return 0
def delivery_charge(total):
# Delivery is free for 20 pounds or more. Otherwise it is 3.
return None
def receipt_line(name, quantity, price):
# Text like: 2 x Cola = 3.0
return ""
Make a free account to save your work, or press Start lab to work through the whole lesson.
Key words crossword
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Exit quiz
Have a go
Exit quiz
6 questionsRemember
Important
Find the features. Describe each one. Break the big ones down. Show it with a block diagram, an information flow diagram, a flowchart, code or words.