On this concept: Formula

Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Computational Thinking · Grade 6-8

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.

system=M1+M2+⋯+Mk

Quick Answer

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 talk to each other only through clear interfaces: the inputs a module accepts and the results it returns. Use it whenever a program grows past a few dozen lines or a team shares the work, for example a game split into input, physics, graphics and sound. Then you can build, test, fix or replace one module without rewriting the others.

Why This Matters

With modules, four people can each build one part of an app at the same time, a bug in the score display can be hunted in the score module instead of across 2,000 lines, and the sound system can be swapped without touching the physics. Without them, every change risks breaking something far away, because everything depends on everything else.

Intuitive Explanation

Think about a school's kitchen, front office and library. Each has one job, and they work together through simple requests: the office tells the kitchen how many lunches are needed, not the names of everyone's teachers, and nobody in the office needs the recipe. A modular program works the same way. A quiz app might have a questions module (load and hand out questions), a scoring module (add up points) and a display module (show things on screen).

The connection between two modules is an interface: what goes in and what comes out. The scoring module might offer addPoints(n) and getTotal(). The display module calls getTotal() and shows the number; it never reaches inside to change the score variable itself. That rule gives modular design its two goals. High cohesion: everything in a module serves its one job. Low coupling: modules depend only on each other's interfaces, not on their insides. If the scoring module later stores points differently, the display keeps working, because getTotal() still returns a number.

Splitting code into several files is not automatically modular. If the display file reads and changes the scoring file's variables directly, the two are tightly coupled: change one and the other breaks. A program in a single file can be fairly modular, too, if it is built from well-separated functions with clear inputs and outputs. What counts is how independent the parts are, not how many files there are.

Think of it like this. Modules are like the parts of a desktop computer: keyboard, mouse and monitor each do one job and plug in through standard ports. You can replace a broken keyboard without opening the monitor, because the port, the interface, stays the same. One way the picture falls short: a software module can be copied and reused in another program for free.

Core idea. Modules can be developed, tested, and replaced independently.


When to Use

Use modular design when a program has several separate jobs (reading input, calculating, displaying, saving), when several people will build it, or when parts will be reused or replaced later. A warning sign that you need it: one change keeps breaking things in unrelated places. If you are only breaking a problem into steps on paper, that is Decomposition. A single named block of code is a Function; a module usually groups several related functions behind one interface.

Tip. Ask: Is a program being organized into separate parts, each with one job, that talk only through defined inputs and outputs?

How to Recognize It

Look at how the finished program is arranged, not at how you first thought about the problem.

  1. Is the program split into parts that could each be built and tested by a different person?

    Yes points to modular design. If you are only breaking the task into steps on paper, with no code parts yet, that is Decomposition.

  2. Does each part hide its inside details and offer a small set of ways to use it?

    Yes is modular design. If the question is only about that set of ways in, the names, inputs and outputs a part promises, it is about Interface.

  3. Is each part bigger than one task, like a whole sound system or scoreboard, often with several functions inside?

    Yes fits a module. If the question is about one named block of code with parameters and a return value, it is about Function.

  4. Could you swap one part for a new version without changing the others?

    Yes is the payoff of modular design. If changing one part forces edits all over the program, the parts are tightly coupled and the design is not modular yet.

Modular Design vs Decomposition vs Function vs Interface

Decomposition often leads to modules, modules contain functions, and interfaces join modules, so the four overlap. Ask whether you are splitting a problem, packaging code into parts, writing one block, or defining how parts connect.

Modular Design

Meaning
Modular design arranges a program as separate parts, each doing one job and used only through its interface.
Key test
The parts are real code that can be swapped, tested or reused separately.
Formula
system=M1+M2+⋯+Mk
Example
A drawing app built from a file module, a brush module and an undo module.

Decomposition

Meaning
Decomposition is breaking a big problem into smaller problems that can each be solved.
Key test
It happens in planning, before or without code; the parts are sub-problems.
Example
Planning a birthday party as guests, food, games and decorations.

Function

Meaning
A function is one named block of code that takes inputs, does a task and can return a result.
Key test
It is a single block; one module can hold many functions.
Example
def area(w, h): return w * h, called as area(3, 4) to get 12.

Interface

Meaning
An interface is the promise a module makes to the rest of the program about what it accepts and returns.
Key test
It describes how to use a part, not how the part works inside.
Example
A sound module offers play(name) and stop(); the rest of the game never sees its code.

Formula & Notation

system=M1+M2+⋯+Mk
A modular system consists of components M1,M2,…,Mk where each Mi exposes an interface Ii and hides its implementation. The coupling between modules should be minimized while cohesion within each module is maximized.

How to read it: Modules are often represented as boxes in architecture diagrams. Arrows between boxes show dependencies. 'High cohesion, low coupling' is the guiding principle.

Worked Examples

Example 1 — Split a weather station into modules

Easy

A weather-station program reads the temperature sensor every minute, converts °C to °F, stores the day's readings, works out the day's high and low, and shows results on a screen. Group these jobs into modules, give each an interface, and say which module changes if the station gets a different sensor.
Show the steps and the answer
  1. Group the jobs by purpose: Sensor (read the temperature), Storage (keep today's readings), Calculations (°C to °F, high and low), Display (show results).

    Each module gets one kind of work, which is high cohesion: the arithmetic is together and the screen code is together.

  2. Give each a small interface: Sensor.read() returns °C; Storage.add(c) and Storage.today(); Calc.toF(c) returns c × 9/5 + 32, plus Calc.high(list) and Calc.low(list); Display.show(text).

    Other modules use only these names, so how each module works inside stays its own business.

  3. Trace one minute: Sensor.read() gives 20; Storage.add(20); Calc.toF(20) = 20 × 9/5 + 32 = 68; Display.show('20 °C / 68 °F').

    Following one reading through the interfaces checks that each module gets what it needs and nothing more.

  4. New sensor: rewrite only the inside of Sensor.read(), keeping it returning °C.

    No other module knows how the sensor works, so the change stays inside one module. That is low coupling paying off.

Answer: Four modules: Sensor (read), Storage (add, today), Calculations (toF, high, low) and Display (show). A new sensor changes only the inside of Sensor.

Takeaway: Group code by job and connect the groups through a few named inputs and outputs, so that a change to one part stays in that part.

Example 2 — Three files, but not modular

Standard

Jay splits his game into player.js, enemy.js and score.js. When an enemy is hit, code in enemy.js does player.health = player.health + 10 and score.points = score.points + 50 directly. Later Jay renames points to total inside score.js, and the game breaks in enemy.js. Is this modular design? How should it be fixed?
Show the steps and the answer
  1. List what enemy.js touches: player.health and score.points, variables that live inside the other two files.

    Reaching into another module's variables means depending on its insides, which is tight coupling.

  2. Explain the break: after the rename, enemy.js still uses score.points, a name that no longer exists.

    A change that should have stayed inside one file broke a different file, the exact problem modules exist to prevent.

  3. Give score.js an interface, addPoints(n), and player.js one, heal(n). enemy.js now calls score.addPoints(50) and player.heal(10).

    enemy.js now knows only what each module offers, not how it stores its data.

  4. Repeat the rename: inside score.js, addPoints(n) becomes total = total + n, and enemy.js needs no change.

    With an interface in between, the inside of score.js can change freely.

Answer: No: three files that share internal variables are tightly coupled. With interfaces (score.addPoints(50), player.heal(10)), renaming inside score.js no longer breaks enemy.js.

Takeaway: Modular design is about independence, not file count. If changing one part's insides breaks another part, they are not yet separate modules.

Example 3 — Find the mistake: the do-everything module

Application

Ella's school-library app has one module, Library, with six functions: searchBooks, checkOut, calculateFine, sendOverdueEmail, drawHomeScreen and playClickSound. She says: "Everything is in one module, so it's simple: there's only one place to look." Then fines start coming out wrong. Find the mistake and correct it.
Show the steps and the answer
  1. List the jobs the six functions do: finding books (searchBooks), loans (checkOut, calculateFine), messages (sendOverdueEmail), and screen and sound (drawHomeScreen, playClickSound).

    Four different jobs in one module is low cohesion; 'one place to look' really means one big place to search.

  2. Split them into four modules: Catalog, Loans, Messages and Screen.

    Each module now does one job, so a bug about fines can only be in Loans.

  3. Test Loans on its own: with a fine of 10 cents a day, calculateFine(4) should return 40 cents and calculateFine(0) should return 0.

    A small module can be tested without drawing screens or sending emails, so the faulty function is quick to pin down.

  4. Connect the modules through interfaces: Messages calls Loans.calculateFine(days) to put the amount in the overdue email.

    Messages uses the fine without containing any fine code, so the fine rule lives in exactly one place.

Answer: One module doing four jobs is low cohesion. Split it into Catalog, Loans, Messages and Screen; the fine bug can then be found and tested inside Loans, where calculateFine(4) should give 40 cents.

Takeaway: If describing a module needs the word 'and' several times, it holds more than one job. Split it.

Where You'll See It

  • LEGO bricks

    Each brick does one simple thing and connects through standard studs, so you can swap or rebuild parts without touching the whole model, just like independent software modules with clear interfaces.

  • Car parts you can replace

    A car's battery, tires, and headlights are separate modules with standard fittings, so a mechanic can replace one without rebuilding the engine, the same benefit modular code gives developers.

  • Group project split into sections

    When a team divides an app into a login part, a scoreboard part, and a settings part with agreed inputs and outputs, each person builds and tests their piece independently, the core idea of modular design.

Common Mistakes

  1. The slip: One big module that does everything is simpler than many small ones.

    Why it's tempting: Fewer files feels tidier and easier to keep track of.

    Instead: Each module should do one well-defined job (high cohesion). A do-everything module is hard to test, reuse, and debug.

  2. The slip: Modules can reach directly into each other's internal details to get things done.

    Why it's tempting: Grabbing a value directly is the quickest way to make it work right now.

    Instead: Modules should communicate only through clear interfaces (loose coupling). Depending on internals creates tight coupling, so a change in one module breaks others.

  3. The slip: Splitting code into files is the same as modular design.

    Why it's tempting: Multiple files look modular, so the structure seems achieved.

    Instead: True modularity needs defined interfaces and independence between modules; files that all depend on each other's internals are just spaghetti spread across files.


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. A quiz app must load questions from a file, show a question, check the answer, keep the score, and show the final result. Group these five jobs into three modules, one responsibility each.

    Hint: Separate storage, game rules, and what appears on screen.

  2. A game has three modules: Input, Physics and Display. A bug report says the ball falls through the floor. Which module do you check first, and why?

    Hint: Which module is responsible for movement and collisions?

  3. Five modules each talk directly to every other module. How many connections is that? How many if each talks only to one central module instead?

    Hint: Each pair of modules is one connection. Count the pairs.

  4. Module Score offers add(points) and getTotal(). The Display module runs Score.total = Score.total + 5 to add a bonus. Rewrite that line so it uses the interface.

    Hint: Use the functions Score offers instead of its stored data.

  5. You break 'make breakfast' into 'toast bread', 'boil eggs' and 'pour juice', with no code yet. Is this modular design or decomposition?

    Hint: Is anything being built as separate code with an interface?

  6. A student writes one module, Game, that reads the keyboard, moves the player, plays sounds, saves high scores and draws the screen. They say: 'One module is simplest, so this is good modular design.' What would you change?

    Hint: How many different responsibilities does Game have?

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

How big should a module be?

Big enough to do one whole job, and small enough that you can describe that job in one short sentence. 'Saves the player's progress' is one job; 'draws the menu, plays music and saves progress' is three, so that module should be split. Programmers call a module that sticks to one job cohesive. If you need a list to describe what a module does, it is probably doing too much.

Are real apps actually built this way?

Yes, at every size. A phone's camera app does not contain its own code for saving files or reading touches; it uses modules that the operating system provides to every app. Large websites go further and run separate services for login, search and payments, so the payment team can update their part on a Tuesday without the search team changing anything.

Does modular design make a program slower?

Slightly, sometimes, but rarely enough to matter. Passing data through an interface instead of reaching straight into another part can add a few extra steps. In return, bugs are easier to find, because you can test each module on its own and see which one fails. For almost every school or real project that trade is worth it; programmers merge parts for speed only after measuring a real slowdown.

Learning Path

Modular Design

You are here

You need Function (named blocks with inputs and outputs), Abstraction (hiding details that do not matter) and Decomposition (splitting a problem into parts) first. Modular design turns those parts into independent pieces of code joined by clear interfaces. Next, Interface looks at how to specify what a module accepts and returns, and Documentation records it so others can use a module without reading its code.

See Also