Dividing Software into Modules
Modular decomposition is the division of a software system into units with explicit boundaries and defined ways to interact. You use it when deciding where program behaviour belongs and which units need to know about one another. A useful decomposition lets you understand a unit locally while still explaining how the complete program works. The aim is manageable relationships, rather than simply producing more files.
Choose a boundary
A module groups related software elements behind a boundary that exposes selected operations and keeps implementation details private. Depending on the language and project, a module might be represented by a package, a language module, or several cooperating source files. A file boundary alone does not establish a useful module boundary.
The module's public interface tells other modules what they can request and what results or errors to expect. Private choices include algorithms, temporary data, and storage arrangements that callers do not need to access directly.
For example, a seat-allocation module could expose an operation that reserves an available seat. Its callers need to understand the reservation result; they need not know how the module searches its internal seat records. Making callers inspect those records would spread knowledge of a private representation across the system.
Place boundaries in a larger system
A system may contain subsystems, which can contain several modules. A component describes an assembly unit and may coincide with a module, a library, or a service. These names depend partly on the design viewpoint; they do not establish one universal size hierarchy.
Viewpoint | Example in an order service | Boundary question |
|---|---|---|
Whole system | Order service | What belongs inside the service being designed? |
Subsystem | Delivery | Which modules collectively provide delivery behaviour? |
Module | Tracking | Which operations are exposed and which details stay private? |
Component | Installed address-checking library | How is this unit connected to the surrounding software? |
Choose the level that answers your design question. A subsystem diagram helps you locate major functions; a module diagram helps you inspect the boundaries between smaller units. Neither diagram automatically describes separate operating-system processes or separate machines.
Read and count dependencies
A direct dependency has a direction. Here, A→B means that A calls B. It does not imply that B calls A. Two modules can also depend on one another, producing two distinct directed relationships.
For n modules, excluding self-dependencies and counting each ordered source–target pair once, the number of possible directed pairs is
Each module has n−1 possible targets. Actual dependencies are the pairs the design uses, not every pair that could exist. The count helps you inspect connections, but it is not a universal quality score: removing a necessary connection may simply prevent the software from working.
A change to private details can remain local when the full public interface contract stays unchanged. Keeping an operation's name while changing its promised behaviour does not satisfy that condition. An incompatible interface change requires you to inspect its callers; callers farther upstream may need verification even when their source code remains unchanged.
What to remember
- Choose boundaries with explicit exposed operations and private implementation details.
- Interpret system, subsystem, module, and component at the stated design level.
- Read dependency arrows in their declared direction.
- Count possible connections separately from actual connections.
- Judge change isolation using the complete interface contract, including behaviour.
Example: Decomposing a Campus Locker Service
Example: A campus locker service accepts delivery requests, chooses lockers, issues collection codes, and sends messages. Its team proposes four modules: Intake, Allocation, Access, and Notification. Each module is a bounded unit with published operations and private implementation details.
Intake validates delivery requests and then asks Allocation to reserve a locker. Allocation stores locker availability privately, asks Access to create a collection code, and asks Notification to send the ready-for-collection message. Access also asks Notification to send a message after collection. These are all the direct dependencies between the four modules. Each arrow below means that its source invokes its target.
a) Assign the software elements
Assign the request-validation rules, the locker-availability records, the code-generation algorithm, and the message-delivery adapter to the proposed modules. Explain whether Intake should update the availability records directly.
Request-validation rules belong in Intake because it accepts and validates delivery requests. Locker-availability records belong in Allocation because it chooses and reserves lockers. The code-generation algorithm belongs in Access, while the message-delivery adapter belongs in Notification.
Intake should request a reservation through Allocation's published operation. Directly updating Allocation's private records would make Intake depend on their representation and could bypass Allocation's reservation behaviour.
The resulting assignment is validation to Intake, availability records to Allocation, code generation to Access, and message delivery to Notification. Intake interacts with Allocation through its exposed operation.
b) Count actual and possible dependencies
How many actual directed dependency pairs does this design contain? How many pairs are possible among the four modules if self-dependencies are excluded and each ordered pair is counted once?
First identify the number of modules: there are ________. Each source module has ________ other modules that could be its target. Because opposite directions are different pairs, multiply rather than dividing the result by two.
Now list the actual pairs: Intake to Allocation, Allocation to Access, Allocation to Notification, and Access to Notification. Repeated calls along any one of these relationships would still count as one pair under the stated rule.
The design contains 4 actual directed pairs out of 12 possible pairs. The unused pairs are possibilities, rather than missing features that the team should automatically add.
c) Locate the caller affected by a change
Allocation replaces its reservation operation with an incompatible operation that requires an additional delivery-size argument. There is no compatibility adapter. Intake can obtain this size from the delivery request without changing its own published contract. Which caller must be updated, and does the graph justify updating Access or Notification?
Start with the operation being changed and identify modules that call its owner. The only arrow into Allocation comes from Intake, so Intake must update its invocation and supply the delivery size.
The arrows from Allocation to Access and Notification point in the other direction. Those modules are services that Allocation invokes; they are not callers of Allocation's reservation operation. Nothing in the stated change requires their interfaces or implementations to change.
Intake is the caller requiring an update. The given information does not justify source edits to Access or Notification. Read the dependency direction before deciding where an interface change propagates. Intake's preserved contract also allows the adaptation to stop there rather than automatically spreading to its own callers.
A timetable application groups closely related room-allocation operations inside one module. Other modules communicate with it through a small public interface and do not access its internal records.
Complete the design description with the conventional terms: strong relatedness among operations within a module is high , while limited dependence between modules is low .
A library-loan module exposes borrow and return operations. Their contract specifies accepted inputs, results, errors, and observable behaviour, but excludes storage details. Which change can remain entirely inside the module while preserving that contract?