Start programming in modules by giving each part of your application one clear responsibility and a small way to communicate with the others. Code organized into modules becomes easier to understand when each boundary tells you where a decision belongs.
Consider a command-line expense tracker. It accepts an amount and category, saves the expense, and shows totals by category. As features grow, parsing input, calculating totals, and reading a file can become tangled. Separate those jobs without building a space station to record a cup of coffee.
Find responsibilities that belong together
Begin by listing what the application does, then group work that changes for the same reason. The expense tracker has three natural areas:
- Command interface: read a user’s command, check its shape, and display results.
- Expense operations: validate an expense and calculate category totals.
- Storage: load and save expenses in a chosen format.
A small fourth piece assembles the parts. In modular programming, a module is a unit with a responsibility and an interface through which other parts use it. Depending on the language and project, that unit might be a file, package, class, or collection of files. The boundary matters more than its container’s name in modular programming.
In a modular approach to programming, expense operations belong together because they express rules about expenses. If the application later disallows negative amounts, that rule belongs there regardless of whether a user enters an expense at a terminal or through a web page. Formatting an error for the terminal belongs at the command interface, where it can be changed without rewriting the validation rule.
Modular code: Give each part a contract
An interface describes what a module offers and what callers can expect. Documented functions, inputs, outputs, and errors can form a contract. For this tracker, the parts might expose these operations:
- The command interface accepts text and turns it into a request to add an expense or show totals.
- Expense operations accept an amount and category, reject invalid values, and calculate totals from a collection of expenses.
- Storage exposes
loadExpenses()andsaveExpenses(expenses), returning data or a clear error if an operation fails.
The command interface can ask the operations module to create a valid expense. The application then asks storage to save the updated collection. To show a summary, it loads expenses, asks the operations module for totals, and displays the result. This sequence gives modularized code recognizable jobs. Modular code is easier to change when callers depend on these promises instead of reaching into another part’s internal variables.
The contracts should answer practical questions. Is the amount expressed in cents or dollars? Here, use integer cents to avoid introducing floating-point rounding into ordinary money calculations. What happens if the file cannot be read? Storage reports an error, and the command interface presents an understandable message. These details let modular code cooperate without guessing at another module’s intentions.
Keep related decisions together
A module has high cohesion when its contents contribute to a closely related purpose. The calculation of totals and the rule that an expense must have a category both concern expense data. A function that chooses terminal colors has a different purpose, even if the same developer wrote it on the same afternoon.
In programming a modular application, cohesion helps maintenance because a change has a likely home. When a new expense rule arrives, you can inspect the operations module and its tests rather than search through input and file-handling code. Programming with modules also makes review more focused: the reader can ask whether a change respects one responsibility before following its effects elsewhere.
This does not mean that every function in a module must be tiny or that every noun deserves a separate module. If two pieces always change together and their separation forces them to exchange many details, consider keeping them together. Start with a boundary that explains the program, then adjust your modular program design as you learn more.
Hide details and control dependencies
Storage might initially use a CSV file. The other parts should ask it to load or save expenses rather than construct paths, split rows, or know column positions. That is information hiding: the storage module owns those choices behind its contract. Code with modular boundaries lets you change the format in one place, provided the replacement still fulfills that contract.
Draw the dependencies as arrows: the assembly code knows which components to connect; the command interface uses expense operations; the application flow uses storage through its load and save operations. Expense calculations should not need to import terminal formatting or CSV parsing. In modular software programming, visible directions reveal when a detail pulls unrelated work into its orbit.
Modular programming also makes testing boundaries practical. Test the expense calculations with a few in-memory records and expected totals. Test storage separately with a temporary file, including a missing or malformed file. Then run an end-to-end test that adds an expense and displays its category total. Each test answers a different question, so a calculation failure points toward the rules rather than the filesystem.
A program organized modularly can still have dependencies; useful parts must talk. For code split into modules, make those dependencies explicit and narrow enough that a storage-format change does not require edits to every caller. If the storage contract changes, its consumers may still need changes. A boundary makes the impact visible; it does not abolish cause and effect.
Check the boundary, not just the filenames
Moving functions into three files does not automatically create modular code. If the command file changes storage’s private variables, or the totals function parses CSV rows, the responsibilities still leak across the seams. Separate files are containers; coherent contracts turn separated code into modules.
Review each call across a boundary. Ask what the caller must know and whether that knowledge belongs to the caller. A modular style of programming keeps a list of expense records visible to the calculation that needs it while keeping CSV delimiters inside storage. A modular approach to code also avoids passing an entire command-line configuration object to a function that only needs an amount and category.
As the tracker grows, module-based programming can accommodate a second interface, such as a web page, by reusing the expense rules. The new interface still needs to translate requests, display errors, and connect to storage. It gains a defined place for those jobs without copying the rules. Such code organized around modules gives each interface a clear role.
Choose one function in your current project, write down its responsibility and the inputs and outputs its callers need, and move unrelated decisions behind a separate boundary.
