Computer Science Grade 9 20 min

2. Decomposition: Breaking Down Problems

Learn how to break down a large problem into smaller, more manageable sub-problems.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Decompose a multi-step problem into a hierarchy of functions. Identify reusable components within a problem and encapsulate them in separate, single-purpose functions. Use parameters and return values to correctly pass data between decomposed functions. Apply the top-down design methodology to sketch a program's structure before writing code. Differentiate between functions with high cohesion (good) and low cohesion (bad). Refactor a large, monolithic block of code into smaller, well-defined functions to improve readability and maintainability. How would you build a complex LEGO castle? 🏰 You wouldn't just dump all the pieces on the floor; you'd build it section by section, following the smaller steps in the instruction booklet. Decomposi...
2

Key Concepts & Vocabulary

TermDefinitionExample Top-Down DesignA problem-solving approach where you start with the main, high-level goal and progressively break it down into smaller, more detailed sub-problems or functions.To create a 'Contact Manager App', you first define the main goal. Then you break it down into `display_contacts()`, `add_contact()`, and `search_contact()`. Then you break `add_contact()` down further into `get_user_input()` and `save_to_file()`. Functional DecompositionThe process of breaking a complex problem or system down into a set of smaller, single-purpose functions that work together.A program to process an online order is decomposed into functions like `validate_payment()`, `check_inventory()`, `calculate_shipping()`, and `send_confirmation_email()`. ModularityThe degree to w...
3

Core Syntax & Patterns

The Single Responsibility Principle (SRP) Every function should have responsibility over a single part of the program's functionality. Use this as a guide when creating functions. If you can describe your function's purpose using the word 'and' (e.g., 'it gets user input AND validates it'), it might be doing too much and should be broken into two separate functions. Top-Down Design Pattern 1. Define the main goal. 2. Identify major sub-tasks and create placeholder functions. 3. Implement each sub-task function, breaking them down further if needed. 4. Integrate and test. This is a structured workflow for tackling a new project. Start by sketching the overall program with function calls, then fill in the details of each function one by one. This...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
You are designing a simple ATM program. The main goal is to 'perform a user transaction.' Following the principle of Top-Down Design, which set of functions represents the best initial, high-level decomposition of this goal?
A.`validate_pin()`, `check_balance()`, `withdraw_cash()`, `display_receipt()`
B.`connect_to_database()`, `read_account_balance()`, `update_account_balance()`
C.`get_card_number()`, `get_pin_from_keypad()`, `dispense_bills()`
D.`calculate_interest()`, `check_for_fraud()`, `update_customer_address()`
Challenging
A teammate writes a function `calculate_final_grade(midterm_score, final_exam_score)` which, internally, calls another function `get_homework_scores_from_user()`. Why does this design violate the principles of good decomposition?
A.The function name does not accurately reflect that it performs user input.
B.It mixes a calculation-focused task with an I/O task, giving it low cohesion and making it hard to reuse or test.
C.The function should not be getting homework scores at all.
D.It's perfectly fine, as it encapsulates all the logic in one place.
Challenging
Two programmers decompose the Text Adventure Game. Programmer A creates functions for each location: `handle_hall()`, `handle_library()`. Programmer B creates functions for each action: `display_room()`, `get_player_input()`, `update_game_state()`. Which approach is generally better according to the tutorial's principles, and why?
A.Programmer A's, because it's easier to add new rooms by just adding a new function.
B.Programmer B's, because it separates the game's logic (updating state) from its presentation (displaying), leading to higher cohesion and reusability.
C.Both are equally good as they both break down the problem.
D.Programmer A's, because the game is organized by location, so the code should be too.

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Advanced Topics

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.