๐Ÿ“˜ CodingMarble Learn

Modular Design: Building Programs from Parts

Modular design means splitting a big program into small, separate parts called modules. Each module does one job and can be written, tested and fixed on its own. Functional decomposition keeps breaking a task into smaller subprograms (functions) until each is easy to code. Classes group data with the methods that use it. Encapsulation hides a module's data so other parts can use it only through a public interface. Good modules have high cohesion (one clear job) and low coupling (few links to others), which makes them reusable in other programs.

๐ŸŽฌ Step-by-step story

  1. Here is one big problem: a whole library app written as one giant block of code. It is hard to read, test or change.
  2. Split it. The app becomes three modules: Members, Books and Loans. Each module has one job.
  3. Split again. Each module breaks into small subprograms, like add() and search(). Breaking a task down like this is functional decomposition.
  4. Now hide the Books data inside a glass shell. Other parts cannot touch it directly. They must use add() and search(). This is encapsulation.
  5. A shop app also needs a list of items. The same Books module plugs in without changes. This is reusability.
  6. Your turn. Tap Split, Join, Encapsulate and Reuse. Count the pieces each time.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

๐Ÿค” Common doubts, cleared

Why not just write one big program? It works.

It may work, but one change can break anything, and nobody can read it all. Small modules are easier to read, test and fix.

How do I know where to split?

Split by job. Each thing (noun) can be a class and each action (verb) a function. If a part does two unrelated jobs, split it.

When do I stop breaking things down?

Stop when each subprogram does one clear task and is short enough to write and test easily.

If the data is hidden, how do other modules use it?

Through public methods like add() and search(). They are the only doors into the module.

Is a module the same as a class?

A class is one kind of module. A module can also be a file, a package or a group of functions.

How can the same module work in a different app?

Because it does not depend on the rest of the first app. It only needs its interface, so it plugs in anywhere.

Decomposing a problem into modules and classes

A module is a separate part of a program with one job. To decompose a problem:

  1. Write what the whole program must do.
  2. List the main things (nouns) in the problem: members, books, loans. Each can become a class that holds its data and its methods.
  3. List the main jobs (verbs): add, search, issue, return. Each becomes a method or function.
  4. Draw a structure chart (a tree): the program at the top, modules below, subprograms below them.

Two quality checks: high cohesion (everything in a module belongs to one job) and low coupling (modules depend on each other as little as possible).

Functional decomposition in subprogram design

Functional decomposition is top-down design: take a task and break it into smaller sub-tasks, then break those again, until each piece is small enough to write as one short subprogram (function, procedure or method).

issueBook(memberId, bookId)
  โ”œโ”€ checkMember(memberId)
  โ”œโ”€ checkAvailable(bookId)
  โ”œโ”€ recordLoan(memberId, bookId, dueDate)
  โ””โ”€ printSlip()

Each subprogram has clear parameters (inputs) and a return value (output). Good rules: one task per function, a name that is a verb, usually less than a screen long, and no hidden use of global variables.

Programmers also use stubs (empty placeholder functions) to test the top level before the lower parts are written.

Data encapsulation in program design

Encapsulation means putting data and the code that uses it together, and hiding the data from the outside. In most languages you mark fields private and give public methods (the interface).

class Account {
  private double balance;
  public void deposit(double x) { if (x > 0) balance += x; }
  public double getBalance() { return balance; }
}

Nobody outside can set the balance to โˆ’500, because the only way in is deposit(), which checks the value. Benefits: data stays valid, the inside can be changed later without breaking other code, and each module is easier to understand.

Reusability in program design

Reusability means writing a module once and using it in many places or programs. Ways to make code reusable:

Reuse saves time, cuts bugs (tested code is used again), and keeps programs consistent. Almost every app uses library modules for things like dates, maths and networking.

Try it

Unplugged: write "Make breakfast" at the top of a page. Break it into 3 modules, then each into 2โ€“3 steps. Circle any step you could reuse for "Make lunch".

In the 3D: press Split twice and count the pieces (1 โ†’ 3 โ†’ 6). Then press Encapsulate and Reuse.

Key formulas and definitions

Worked examples

1. Decompose a school report-card program into modules.

Modules: Students (store names and IDs), Marks (enter and check marks), Calculations (total, average, grade), Report (format and print). Each has one job, so each can be tested alone.

2. Break the task 'calculate a student's grade' into subprograms.

readMarks(id) โ†’ total(marks) โ†’ average(total, count) โ†’ gradeFor(average) โ†’ return grade. Each function takes inputs and returns one result.

3. Why should 'balance' in a BankAccount class be private?

So no other code can set it to a wrong value. All changes go through deposit() and withdraw(), which check the amount. The data stays valid.

4. A function computes area = 3.14 ร— 5 ร— 5. How can you make it reusable?

Add a parameter: area(r) returns 3.14159 ร— r ร— r (or uses the language's pi). Now any radius works and the function can be put in a maths module.

5. Module A reads module B's variables directly, and B reads A's. What is wrong and how do you fix it?

Coupling is high: changing one breaks the other. Fix: make the variables private and pass data through method calls with parameters and return values.

Common mistakes

Practice quiz

1. Breaking a task into smaller sub-tasks is called:
2. Encapsulation mainly means:
3. A well-designed module has:
4. Which makes a function more reusable?
5. A tree diagram showing modules and their subprograms is a:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is modular design in programming?

Building a program from small, separate modules, each with one job and a clear interface, so they can be written, tested and reused on their own.

What is the difference between decomposition and encapsulation?

Decomposition splits a problem into parts. Encapsulation hides each part's data behind its methods.

What are cohesion and coupling?

Cohesion is how focused a module is on one job (want high). Coupling is how much modules depend on each other (want low).

Where this is taught

Canada (Ontario)Grade 12C. Designing Modular Programs

Learn first

Learn next

Related lessons

All Computer Science lessons