AQA 7517, section 4.4.1.10
Composition is the opposite of decomposition. Decomposition breaks a problem down into smaller parts. Composition builds things up by combining simple parts into more complex ones. There are two kinds you need to know.
Composing procedures
A compound procedure is built by combining simpler procedures. Each simple procedure does one job, and the compound one uses them together.
def square(n):
return n * n
def add(a, b):
return a + b
def sum_of_squares(a, b):
return add(square(a), square(b)) # a compound procedure
print(sum_of_squares(3, 4)) # 25
sum_of_squares has no calculation of its own. It is composed from square and add. It could itself be used inside an even larger procedure:
def hypotenuse(a, b):
return sum_of_squares(a, b) ** 0.5
print(hypotenuse(3, 4)) # 5.0
This way of building layers (square and add, then sum_of_squares, then hypotenuse) is how large programs are constructed from small, tested pieces. Each level hides the detail of the one below it.
Composing data objects
A data abstraction can be built by combining data objects into compound data.
- A record for a person combines a name (string), an age (integer) and an address (which could itself be a record).
- A list of records combines many records.
- A tree is built by combining nodes, where each node holds a value and links to other nodes.
class Node:
def __init__(self, value):
self.value = value
self.left = None # links to other Node objects
self.right = None
root = Node(8)
root.left = Node(3)
root.right = Node(10)
Each Node is a simple data object. Joining them together composes a tree, a more complex data structure, that can be used through its own operations (insert, search, traverse).
In object-oriented terms, this is also seen in composition (4.1.2.3), where a Car object has an Engine object.
Decomposition and composition together
| Decomposition | Composition | |
|---|---|---|
| Direction | Top-down: big to small | Bottom-up: small to big |
| Question | "What parts does this problem have?" | "How can I combine the parts I have?" |
| Result | A set of sub-problems | A larger procedure or data structure |
A typical project uses both: decompose the problem into tasks, write each small part, then compose them to make the whole program.
Worked example
Task: build a procedure that gives a customer's total bill with tax, using smaller procedures.
def subtotal(prices):
return sum(prices)
def add_tax(amount, rate):
return amount + amount * rate
def bill(prices, rate):
return add_tax(subtotal(prices), rate)
print(bill([10.0, 5.0, 5.0], 0.2)) # 24.0
bill is a compound procedure composed from subtotal and add_tax. Each of those is simple, easy to test and reusable on its own. Here the prices total 20.0, and adding 20% tax gives 24.0.
Exam tip: give both kinds if asked about composition: compound procedures built from simpler procedures, and compound data built from simpler data, with a tree as an example.
Check yourself
What is a compound procedure?
A procedure that is built by combining simpler procedures.
Give an example of building a compound data object
A tree built by combining nodes, or a record built from several fields (possibly including other records).
How does composition differ from decomposition?
Decomposition breaks a problem down into smaller parts. Composition builds more complex procedures or data out of simpler parts.