Modularity
Dividing a system into parts with clear responsibilities.
A modular system has parts that can be understood, tested and changed separately. Deep modules hide a lot of complexity behind a small interface. Modularity is what lets a product grow without every change touching everything.
Why it matters for a product engineer
A modular system has parts that people can understand, test and change separately. Modularity is what lets a product grow without every change touching everything. For a product engineer it decides how fast the next feature ships, and how much of the system you must hold in your head to ship it.
How to do it
- Organise the code by product concepts, such as orders, billing or search, at the top level. Inside each module, separate presentation, logic and data.
- Give each module a small public interface, and keep the rest private.
- Aim for deep modules: a lot of useful work behind a few simple functions.
- Make the allowed dependencies explicit, and check them automatically in CI.
- Start with one well-structured application. Split it into services only when the boundaries are clear and stable.
- When a change touches many modules, ask whether the boundaries are in the wrong place.
Common mistakes
- Top-level folders by technical type, such as models, views and controllers, so one feature is spread across the whole codebase.
- Modules that call each other’s internals.
- Services split too early, before anyone knows where the boundaries are.
- Shallow modules: many small pieces that each do almost nothing.
Example
In early 2017, Shopify began to divide its large Rails application into components without leaving the monolith. The team listed about 6,000 Ruby classes in a spreadsheet, labelled each with a component, and moved the files in one large pull request. Then a tool called Wedge, built from the call graph in CI, showed every call that crossed a boundary without going through a public interface.
Shopify's code before and after componentization
| Before | After 2017 | |
|---|---|---|
| Top level | Models, views and controllers | Orders, shipping, inventory, billing |
| Each part | Spread across the application | Its own small Rails application |
| Calls between domains | Anything could call anything | Only through a public interface |
| Checked by | – | Wedge, from the call graph in CI |
Shopify kept one codebase and one deploy, and drew the boundaries inside it. The checks make a broken boundary visible, not only a design rule.
ReportedShopify Engineering, 21 Feb 2019
Sources: Presentation domain data layering (Martin Fowler) ↗ · Monolith first (Martin Fowler) ↗ · Deconstructing the monolith (Kirsten Westeinde, Shopify Engineering) ↗
Resources
2- articleStart here
Presentation domain data layering ↗
Martin Fowler · 7 minThe most common way to separate concerns in an application, and why to organize by feature at the top level.
- articleStart here
Monolith first ↗
Martin Fowler · 2015 · 6 minWhy most successful systems start as one well-structured application and split only when boundaries are clear.