Courses / Computer Science I
Programming II

Modular Decomposition and Reuse

Computer Science I 186 words Free to read

Modular Decomposition

As programs grow, modular decomposition splits them into modules: self-contained units with a public interface (what others use) and private internals.

PrincipleDescription
High CohesionEach module has one clear responsibility.
Low CouplingMinimal, well-defined dependencies between modules.
DRY PrincipleFactor shared logic into one place to prevent code drift.
Pitfall: Copy-pasting logic instead of factoring it creates duplicate copies that inevitably drift apart, causing subtle, inconsistent bugs.

Reuse by Extension

A module is reused by being called, but inheritance offers a second route. A subclass inherits fields and methods from a superclass, adding or replacing what differs.

Pitfall: Reaching for inheritance just to share code. Inheritance creates tight coupling; use composition (holding an object as a field) when the true relationship is "has a" or "uses a".
Dynamic dispatch: one call site, routed by what actually arrived

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

12practice questions
2interactive scenes

Programming II