AQA 7517, section 4.4.1.9
Procedural decomposition means breaking a problem into a number of sub-problems, so that each sub-problem accomplishes an identifiable task, which might itself be further subdivided.
It is the "divide" part of "divide and conquer". A large problem can feel impossible, but a set of small, clear tasks is manageable.
How it works
- Start with the whole problem.
- Split it into a few main tasks.
- If a task is still too complicated, split it again.
- Stop when each piece is simple enough to write as one subroutine.
This is the top-down design approach used in the structured approach to programming (4.1.2.2), and the result can be drawn as a hierarchy chart.
Example: a quiz program
- Run quiz is the whole problem.
- It decomposes into three tasks: load the questions, ask them, and show the results.
- Ask questions is still complex, so it decomposes again into get answer and check answer.
Each box is an identifiable task, and each can become a subroutine:
def load_questions(): ...
def get_answer(): ...
def check_answer(given, correct): ...
def ask_questions(questions): ...
def show_results(score, total): ...
Why decompose?
- Manageable: each part is small and easy to understand.
- Teamwork: different people can write different parts at the same time.
- Testing: each subroutine can be tested separately, which makes errors easier to find.
- Reuse: a subroutine such as
check_answercan be used again elsewhere. - Maintenance: a change usually affects only one part.
Worked example
Task: decompose the problem "write a program to manage a library's book loans".
Level 1, the whole task: Manage loans.
Level 2, the main tasks:
- Add a new book
- Register a member
- Lend a book
- Return a book
- Report on overdue books
Level 3, subdividing Lend a book:
- Check the member exists
- Check the book is available
- Record the loan and due date
- Update the book's status
Each of the level 3 tasks is simple enough to write as a single subroutine. Notice that every sub-problem has a clear, identifiable job, and the tasks might be split further if needed.
Exam tip: a decomposition answer should show several smaller tasks, each with a clear purpose, and should show that tasks can be broken down more than once.
Check yourself
What is procedural decomposition?
Breaking a problem into sub-problems, each of which accomplishes an identifiable task, and which may itself be broken down further.
Give two advantages of decomposing a problem
Any two of: each part is easier to understand; different people can work on different parts; each part can be tested separately; parts can be reused; changes affect only one part.
When should you stop decomposing?
When each sub-problem is simple enough to be written as a single subroutine.