Separation of Concerns
The most important design idea of all: keep different responsibilities in different places. When each part does one job, you can understand, test and change each without touching the rest.
If you learn one design idea, learn this one: separation of concerns - keeping different responsibilities in different, well-defined places. It underlies almost every other design principle. When each part of your program does one clear job and doesn't meddle in others', you can understand, test and change each part on its own. When responsibilities are all tangled together, changing anything means understanding everything. This session covers the single most valuable structuring principle.
One part, one responsibility
Give each function, class or module a single, clear responsibility - one reason to change. Mixing responsibilities is what creates tangle.
# tangled: one function does input, logic, storage, and display
def handle_signup(form):
# ...validate the form...
# ...hash the password...
# ...write to the database...
# ...send a welcome email...
# ...render the response...
# separated: each concern its own piece
validate(form); user = create_user(form); email_welcome(user); render(user)Layers: keep concerns apart
At the bigger scale, apps are often organised into layers, each a concern, with clear boundaries between them.
| Layer | Its one concern |
|---|---|
| Presentation | Showing things / the interface |
| Logic (business rules) | What the app actually does |
| Data / storage | Persisting and retrieving data |
| (They talk through clear boundaries) | Not reaching into each other |
Keeping presentation, logic and data separate - so your business rules don't know or care whether data is in a file or a database, and your display doesn't contain business rules - is what lets each evolve independently. You can redesign the interface without touching the logic, or change the storage without touching either. This is why the web sessions separated routing, templates and data - it's separation of concerns in action.
Divide responsibilities cleanly
- Separation of concerns - distinct responsibilities in distinct places - underlies all design.
- Single responsibility - each piece has one job, one reason to change.
- Layers keep presentation, logic and data apart, talking through clear boundaries.
- The payoff: understand, test and change each part without disturbing the rest.
๐ฏ Key takeaways
- Separation of concerns - keeping distinct responsibilities in distinct places - is the most important design idea.
- Give each piece a single responsibility: one clear job, one reason to change.
- At scale, layers (presentation, logic, data) keep concerns apart, talking through clear boundaries.
- The payoff is that you can understand, test and change each part on its own, without disturbing the rest.
Frequently asked
How do I decide where one 'concern' ends and another begins?
This is the practical heart of applying separation of concerns, and while it takes judgement, there are reliable guides. A 'concern' is a distinct responsibility or reason-to-change - a coherent job the code does - so the question becomes 'what are the different jobs happening here, and could they change for different reasons?' A powerful lens is the single-responsibility idea: each piece should have one reason to change. If you can describe a function or module's job in a simple sentence without using 'and', it's probably well-separated; if describing it requires 'it validates input AND saves to the database AND sends an email AND formats the response', those 'and's mark separate concerns that should likely be split, because each could change independently (the validation rules change, or you switch databases, or the email format changes) and you don't want a change to one to risk the others. Another guide is to recognise the classic categories of concern that recur in most apps and tend to belong apart: input/validation, business logic (the actual rules and decisions), data storage/retrieval, presentation/display, and cross-cutting things like logging. When you see these mixed in one place, that's usually a separation opportunity. A related test is the 'reasons to change' one: list the different reasons this code might need to be modified in future; if there are several unrelated reasons, it's holding several concerns. You can also notice it by what a piece needs to know: code that has to understand both how to compute a discount and how to format currency and how to write to a file is juggling multiple concerns; each of those is knowledge that could live in its own focused place. Practically, you don't have to perfectly separate everything up front - a good rhythm is to build it working (perhaps with concerns mixed), then notice where responsibilities have clumped together and pull them apart, especially when a piece is getting long, hard to test, or hard to change. Signs you've drawn the boundaries well: each resulting piece is understandable on its own, has a clear name describing its single job, and can be tested in isolation; you can change one concern (swap the database, restyle the output) without touching the others. Signs you've over-separated (which is possible) include tiny fragments that don't stand alone meaningfully and excessive indirection where you're jumping through many pieces to follow simple logic - separation should reduce complexity, not manufacture it, so if splitting something makes it harder to understand, you've gone too far. So: identify the distinct jobs (watch for 'and's and the classic categories), give each its own piece with one reason to change, and adjust with experience - separating when concerns clump and combining when you've fragmented too finely. The judgement sharpens as you build and feel which separations make change easier. The goal is always changeability: draw boundaries where they let you modify one thing without disturbing others, which is exactly where distinct concerns naturally lie.
Doesn't splitting everything into separate pieces just make the code more spread out and harder to follow?
It can, if overdone, but done well, separation makes code easier to follow, not harder - the key is that good separation reduces what you have to hold in your head at once, while bad or excessive separation scatters things pointlessly. Here's the distinction. Well-separated code lets you understand and work with one piece at a time: when concerns are properly divided, you can look at the validation code without needing to understand the database code, reason about the business logic without the presentation details, and change one part while trusting the others - so although the code is in more pieces, you only ever need to comprehend the relevant piece, which is far less mental load than a giant tangled function where everything is intertwined and you must understand all of it to change any of it. In that sense, separation is a tool for managing complexity by letting you focus, which makes following the code easier. What makes separation feel like harmful spreading-out is when it's done badly: fragmenting code into tiny pieces that don't correspond to meaningful responsibilities, so you're constantly jumping between many trivial fragments to follow simple logic (excessive indirection); or separating along the wrong lines, so related things end up scattered and you have to gather pieces from everywhere to understand one behaviour. That's not good separation of concerns - it's poor structure that happens to be split up. Good separation groups things by responsibility so that each concern is cohesive (the related stuff is together) and the pieces are loosely coupled (they don't need to know each other's internals), which means you find what you need in a sensible place and can work locally. The aim is high cohesion within pieces and loose coupling between them - each piece makes sense on its own and connects to others through clear, minimal boundaries. When separation achieves that, navigating the code is easier because it's organised by what things do, and you can dive into just the part you care about. So the answer is about doing it well and in proportion: separate along real responsibility lines (not into meaningless fragments), keep each piece cohesive and clearly named, and don't add indirection that doesn't earn its keep. Over-separation and mis-separation do harm readability, which is a real caution against blindly splitting everything - but proper separation of concerns is what makes large codebases followable at all, precisely because it means you never have to understand the whole thing to work on a part. If a separation makes the code harder to follow, that specific separation is wrong (too fine, or along the wrong boundary) and you should reconsider it - but the principle itself, applied with judgement, is what keeps growing code comprehensible. Think of it as organising, not scattering: a well-organised set of focused pieces is easier to navigate than one big undifferentiated mass, just as a tidy house with things in sensible places is easier than one big pile, even though the tidy version has 'more places'. The measure, as always, is changeability and comprehension: good separation improves both by letting you focus locally; if a split worsens them, that split is a mistake, not evidence against separating concerns.
Related lessons
Why Design Matters
Any code can work once. The question that separates junior from senior is whether it can sโฆ
Software Design & ArchitectureModelling with State Machines
A huge amount of app logic is really about *states* and the moves between them. Modelling โฆ
Web Development BasicsHow Servers Build Pages
Static pages are just files; real apps *generate* pages from data. This is where your Pythโฆ