OCR H446, section 2.1.1 (b)
Why bother with abstraction? Real problems are huge and messy. Without abstraction, they would be impossible to understand, program or finish.
Reasons abstraction is needed
1. To manage complexity
A real situation has far more detail than a person (or a computer) can handle. A weather system has millions of interacting factors. Abstraction removes what isn't relevant, so the problem becomes small enough to understand and solve.
Imagine writing a program for a taxi firm by modelling every atom of the car. It would be absurd. Only the location, availability and capacity matter.
2. To focus on what is important
Abstraction lets the programmer concentrate on the essential features needed for this problem, so they don't waste time on the rest, and the solution isn't confused by irrelevant detail.
3. To save development time, memory and processing
A simpler model needs less data to store and less processing to run. A weather forecast can't use a model that tracks every air molecule, because it would never finish in time. A simplified model can produce a useful forecast fast enough.
| Detailed model | Abstract model | |
|---|---|---|
| Data stored | Very large | Small |
| Processing time | Very long | Short |
| Cost | High | Lower |
It also saves development time: a simpler model means less to design, write and test, so the program is finished sooner and costs less to build.
4. To make problems solvable
A problem may only become solvable by computer once the unneeded detail has been taken away. Turning a road map into a graph lets you use a standard shortest-path algorithm, which wouldn't be possible on the raw map.
5. To allow reuse
An abstract solution applies to many similar problems. A function that sorts a list works on any list of comparable items. A graph algorithm works whether the graph is a road map, a computer network or a social network. Solving the general problem once saves repeating the work.
6. To hide complexity from users and other programmers
Abstraction means a programmer can use a feature without knowing how it works:
- Call
math.sqrt(x)without writing the square root algorithm. - Use an API to take payments, without knowing how the bank works.
This lets people build on the work of others, and makes software easier to use.
7. To let teams work together
Large programs are made by teams. If each part has a clear interface (what it does, not how), people can work on different parts at the same time, and change the inside of one without affecting the others.
8. To make a program easier to change and maintain
If the details are hidden behind an interface, they can be changed or improved without having to alter everything that uses them. A stack that is changed from an array to a linked list needs no changes to the code that uses it.
Abstraction in action: decomposition and layers
- Decomposition (2.2.2 (c)) splits a problem into parts, and abstraction says what each part must do, without worrying how.
- Layers (1.2.1, 1.3.3) are abstractions stacked on one another, so that no one has to understand the whole system.
Costs of abstraction
Abstraction isn't free. It has some drawbacks:
- A model is less accurate than reality, and may give wrong answers if an important detail has been left out (see 2.1.1 (c)).
- Extra layers can add overhead, such as slower processing.
- A poor abstraction (which keeps the wrong details) can make the problem harder.
Worked example
Task: explain why a satnav program uses a simplified model of the road network instead of detailed maps.
- A road network has vast amounts of detail: every kerb, building and lane. Storing and searching it all would need huge memory and processing power, and would be too slow to give a route in time.
- By abstracting it to a weighted graph of junctions and roads (with distances and speed limits), the program keeps only what it needs to find a route, and can use a standard algorithm (such as Dijkstra's or A*).
- The same abstraction can be reused for any city. And the map data can be updated without changing the routing code.
Exam tip: give reasons and link them to the situation: it reduces complexity, saves memory and processing time, makes the program quicker to develop, lets you reuse solutions, and allows people to work on separate parts. Always mention a trade-off: the abstraction loses some accuracy.
Check yourself
Give two reasons why abstraction is needed
Any two of: to reduce complexity; to focus on what matters; to save memory and processing time; to make the program quicker to develop; to make problems solvable; to allow solutions to be reused; to let teams work on separate parts.
How does abstraction save processing time?
A simplified model has less data to store and process, so calculations are quicker than with a fully detailed model.
How does abstraction help teams work together?
Each part has a clear interface that says what it does without exposing how, so different people can work on different parts at the same time, and change the inside of one without affecting the others.
Give one disadvantage of abstraction
The model is less accurate than reality, and may give wrong answers if an important detail has been left out.