Modular Design CS Thinking Example 4
Follow the full solution, then compare it with the other examples linked below.
Example 4
hardExplain 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.
Solution
- 1 Step 1: Cohesion = how closely related the functions within a module are. High cohesion means a module does one thing well. Coupling = how much modules depend on each other. Low coupling means modules can be changed independently.
- 2 Step 2: Poor design example: a 'Utilities' module that handles file reading, email sending, and date formatting (low cohesion โ unrelated tasks). If the email function directly modifies variables in the file-reading function (high coupling), changing email breaks file reading.
- 3 Step 3: Better design: separate FileHandler, EmailService, and DateFormatter modules, each with a clear interface and no shared internal state.
Answer
High cohesion: each module does one thing. Low coupling: modules are independent. A 'Utilities' grab-bag module violates both principles.
Coupling and cohesion are the key metrics for evaluating modular design quality. They directly affect how easily software can be maintained, tested, and extended over its lifetime.
About Modular Design
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.
Learn more about Modular Design โMore Modular Design Examples
Example 1 easy
A student writes a 200-line program as one big block of code. Suggest how they could improve it usin
Example 2 mediumA game program needs: a menu system, a game loop, a scoring system, and a high-score table. Design a
Example 3 mediumList three benefits of modular design and give a specific example of each benefit in the context of