CIE AS & A Level Computer Science, section 9.1 (Computational thinking skills)
What decomposition is
Decomposition means breaking a large problem into smaller sub-problems, and keeping dividing until each part is simple enough to solve. Each sub-problem can become a module of the program: a procedure or a function.
Benefits
- Each part is easier to understand, write and test.
- Different programmers can work on different modules at the same time.
- Modules can be reused in other programs.
- Errors are easier to locate, because a fault is usually in one module.
- The program is easier to maintain.
Worked example: a school quiz program
Problem: run a 10-question quiz and report the score.
- Get the player's name.
- Ask each question (repeat 10 times).
- Choose a question.
- Display it and read the answer.
- Check the answer and update the score.
- Display the score and a message.
Each numbered step can become its own procedure or function.
def get_name():
return input("Name: ")
def check_answer(given, correct):
return given.lower() == correct.lower()
def show_result(name, score):
print(name, "scored", score, "out of 10")
Decomposition and abstraction
Decomposition divides the problem; abstraction hides the detail of each part. A module is used through its interface (name, parameters and return value) without needing to know how it works.
Structure chart
The result can be shown as a structure chart, with the main program at the top and its modules below (see the section on program design).
Common mistake: stopping decomposition too early, leaving a "sub-problem" that is still a big task. Keep dividing until each part is a single, clear job. Also, each module should have one purpose, so it can be tested separately.
Exam tip: when asked to decompose a problem, give a numbered list of sub-tasks, then divide the larger ones again. Say which parts would be procedures and which functions (those that return a value).
Check yourself
What is decomposition?
Breaking a problem down into smaller sub-problems that are easier to solve.
Give two benefits of decomposing a problem.
Any two: easier to understand and test, can be shared between programmers, modules can be reused, errors easier to find.
What can each sub-problem become in a program?
A module, such as a procedure or a function.