Modular Design Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Modular Design.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

Concept Recap

Modular design is the practice of structuring a program as a set of independent, self-contained modules, each responsible for a single, well-defined task. Modules communicate through clear interfaces, making the system easier to build, test, debug, and maintain.

LEGO blocksβ€”each piece does one thing and connects to others in standard ways.

Read the full concept explanation β†’

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

What to Focus On

Core idea: Modules can be developed, tested, and replaced independently.

Common stuck point: Modules should have minimal dependencies on each other (loose coupling).

Sense of Study hint: When applying modular design, first identify the distinct responsibilities in your program (e.g., input handling, data processing, display). Then create a separate module for each responsibility with a clear interface. Finally, ensure modules communicate only through their interfaces, not by accessing each other's internal data.

Worked Examples

Example 1

easy
A student writes a 200-line program as one big block of code. Suggest how they could improve it using modular design.

Solution

  1. 1
    Step 1: Identify distinct tasks in the program (e.g., reading input, processing data, displaying results).
  2. 2
    Step 2: Break each task into a separate function or module with a clear name (e.g., readData(), calculateAverage(), displayResults()).
  3. 3
    Step 3: The main program calls these modules in sequence. Each module can be developed, tested, and debugged independently.

Answer

Break the program into separate functions for each task. Each function handles one responsibility and can be tested independently.
Modular design divides a program into self-contained, reusable components. This makes code easier to read, test, debug, and maintain β€” especially important as programs grow larger.

Example 2

medium
A game program needs: a menu system, a game loop, a scoring system, and a high-score table. Design a modular structure showing how these components interact.

Practice Problems

Try these problems on your own first, then open the solution to compare your method.

Example 1

medium
List three benefits of modular design and give a specific example of each benefit in the context of a team building a web application.

Example 2

hard
Explain the concepts of coupling and cohesion in modular design. Why do we want low coupling and high cohesion? Give an example of a poor design that violates these principles.

Background Knowledge

These ideas may be useful before you work through the harder examples.

functionabstractiondecomposition