Why a map has no trees
Look at a train map. It shows stations and lines. It does not show streets, trees or shops.
The map is useful because it leaves things out. If it showed everything, you could not read it.
Programmers do the same with a problem. Abstraction means keeping the details that matter and leaving out the rest.
Key words
| Word | What it means | Example |
|---|---|---|
| Decomposition | Breaking a big problem into smaller parts. | a shop: basket, payment |
| Pattern recognition | Finding what repeats or looks alike. | two apps need a login |
| Abstraction | Keeping the details that matter and leaving out the rest. | a train map |
| Input | Data that goes into a solution. | age of a passenger |
| Output | Data that comes out of a solution. | the fare to pay |
| Constant | A value that never changes. | the child fare |
| Variable | A name that holds a value. The value can change. | the age of a passenger |
| Outcome | The result we want at the end. | the passenger pays the right fare |
The cells students fill in are left blank in this preview.
Keep it or drop it?
Have a go
Keep it or drop it?
3 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.
The purpose of abstraction
Abstraction helps you focus. A real problem has hundreds of details. Most do not matter for your solution.
It also makes a solution easy to reuse. A solution that ignores small details works for many cases.
The three tasks of abstraction
| Task | What you do | Bus ticket app |
|---|---|---|
| 1 Find what you need | List the information the solution must have. | Age, route, number of journeys |
| 2 Filter out the rest | Drop details that do not change the answer. | Bus colour, bus logo |
| 3 Hide how it works inside | Let people use a part without seeing how it works. | You press Buy. You do not see the payment code. |
The cells students fill in are left blank in this preview.
Try it: hide the details
price = 2.4567
print(round(price, 2))
# round() hides the maths inside. You only say what you want.
# Change 2 to 1 and run it again.
Six questions to ask
To use abstraction on a problem, answer six questions. Look at the table. It uses a bus fare calculator.
Abstraction for a bus fare calculator
| Question | Answer for the bus fare calculator |
|---|---|
| What inputs do we need? | The age of the passenger. The number of journeys. |
| What outputs and outcomes do we expect? | The cost to pay. The outcome: the passenger pays the right amount. |
| What will vary? | The age and the number of journeys change for each passenger. |
| What will stay constant? | The child fare is 1.00. The adult fare is 2.00. |
| What are the key actions? | Choose the right fare. Multiply it by the number of journeys (fare x journeys). |
| What repeats? | The same sum for every journey and every passenger. |
The cells students fill in are left blank in this preview.