๐Ÿ›๏ธ
๐Ÿ›๏ธ Software Design & Architecture ยท ARCH-101 ยท Session 4 of 6

Decoupling with Events

When everything is directly wired to everything, one change ripples everywhere. Events let parts of your app talk without knowing about each other โ€” a powerful way to keep pieces independent.

โฑ 9 min read๐Ÿ“… Updated 2026-08-06๐Ÿ“Š Tables, charts & examples

As an app grows, a quiet problem creeps in: everything becomes directly connected to everything. The signup code calls the email code, the logging code, the analytics code... and now changing any of them means touching signup, and signup knows about them all. This tight coupling makes change hard. Events are a powerful cure: parts of your app communicate by announcing and listening, without knowing about each other directly. This session covers decoupling - keeping pieces independent.

๐Ÿ“Œ Worked example
The signup function that knew too much. A signup function started simple: save the user. Then it grew - send a welcome email, log the event, update analytics, add to the newsletter, notify the sales team - until it was a tangle that depended on half the system, couldn't be tested without all of it, and had to be edited every time anything new should happen on signup. The fix was one idea: signup stops calling everything and just announces 'user.signed_up'; each reaction subscribes independently, elsewhere. Now signup knows nothing about email or analytics, adding a new reaction touches only the new code, and signup is simple and testable again. Announcing instead of calling turned a tightly-coupled knot into loosely-coupled, independent pieces.

The cost of tight coupling

Coupling is how much pieces depend on each other. When the signup code directly calls everything that should happen on signup, it's tightly coupled to all of them - and that's fragile.

# tightly coupled: signup knows about (and calls) everything
def signup(user):
    save(user)
    send_welcome_email(user)     # signup now depends on email
    log_event(user)              # ...and logging
    update_analytics(user)       # ...and analytics
    add_to_newsletter(user)      # ...and newsletter - it keeps growing
๐ŸŽฏ
Tight coupling means one change forces many. Every time you add something that should happen on signup, you edit the signup function, and it grows to know about more and more of the system. Worse, signup can't be understood or tested without all those dependencies. The pieces are entangled: they can't change or be reused independently. Loose coupling - pieces depending on each other as little as possible - is a core goal of good design, because it's what lets you change one thing without a cascade.

Events: announce, don't call

Instead of signup calling everything, it announces that a signup happened, and any interested piece listens. Now signup knows nothing about email, logging or analytics.

from larzevents import EventEmitter
bus = EventEmitter()

# elsewhere, independently, things subscribe:
bus.on("user.signed_up", send_welcome_email)
bus.on("user.signed_up", log_event)
bus.on("user.signed_up", update_analytics)

def signup(user):
    save(user)
    bus.emit("user.signed_up", user)   # just announce it
Directly callingAnnouncing an event
signup depends on every reactionsignup depends on nothing extra
Add a reaction -> edit signupAdd a reaction -> just subscribe
Can't test signup in isolationsignup is simple and testable
Tightly entangledLoosely coupled, independent

This is the observer/pub-sub pattern (via <a href='/stack/larzevents/'>larzevents</a>). The emitter decouples the thing that happens from the things that react to it, so each can change independently. Use it when one event should trigger several independent reactions - a very common situation.

Keep pieces independent

  • Coupling is how much pieces depend on each other; tight coupling makes change ripple.
  • Events let a piece announce what happened without knowing who reacts.
  • Listeners subscribe independently - add a reaction without touching the source.
  • Loose coupling lets you change, test and reuse pieces on their own (larzevents).

๐Ÿงฉ Do it with LarzOS

Turn this lesson into action with the exact free tools and AI workflows for the job:

๐ŸŽฏ Key takeaways

  • As apps grow, tight coupling (everything directly connected) makes one change ripple everywhere.
  • Coupling is how much pieces depend on each other; loose coupling is a core goal of good design.
  • Events let a piece announce what happened without knowing who reacts; listeners subscribe independently.
  • The observer/pub-sub pattern (larzevents) decouples an event from its reactions, so each can change on its own.

Frequently asked

If events decouple things, why not use them for everything?

Events are a powerful decoupling tool, but using them everywhere would be a mistake, because the very indirection that makes them valuable for decoupling also makes control flow harder to follow - so they're right for specific situations, not a universal default. The benefit of events is that the announcer doesn't know who's listening, which loosens coupling; but that same property means that when you read the code that emits an event, you can't directly see what will happen in response - the reactions are registered elsewhere, so following 'what does this actually do?' requires finding all the listeners, which can be scattered. For simple, direct relationships where one piece just needs to call another and it's clear and stable, a plain direct call is more readable and easier to follow than an event - you can see exactly what happens. Overusing events turns straightforward logic into a confusing web where it's hard to trace cause and effect, sometimes called 'event spaghetti', which is its own maintainability problem. So events earn their place in particular circumstances: when one thing happening should trigger several independent reactions (like signup triggering email, logging, and analytics), especially reactions that are optional, vary, or are added over time - here events shine because they let you add or remove reactions without touching the source, and keep the source ignorant of a growing list of consequences; when you want to decouple modules that shouldn't depend on each other directly (so they can be developed, tested, and changed independently); and when the reactions are genuinely independent of the core action (the signup should succeed whether or not analytics is listening). In those cases the decoupling is worth the indirection. But for simple, direct, one-to-one relationships, or where clear traceable control flow matters more than decoupling, direct calls are better - clearer and simpler. The judgement is to use events where their decoupling benefit is real and needed (one-to-many, independent reactions, modules that shouldn't be entangled) and to prefer direct calls where simplicity and traceability win (straightforward, stable, direct relationships). A good rule of thumb: reach for events when you catch a piece of code accumulating knowledge of many unrelated things it must trigger (like a signup function growing to call email, logging, analytics, newsletter...) - that's exactly where decoupling via events helps; but don't introduce events to replace a simple, clear direct call just for the sake of decoupling, because you'd be trading readability for a flexibility you don't need. Like all patterns, events are a tool for a purpose, not a default to apply universally - the skill is recognising the situations (one-to-many, independent, evolving reactions; modules to keep independent) where their benefit outweighs their cost, and using plain direct calls elsewhere. Decoupling is valuable, but so is being able to follow your code's flow, and good design balances both by applying events where decoupling genuinely matters rather than everywhere.

This sounds like it could make debugging harder - if I can't see what an event triggers, how do I follow the code?

That's a genuine and important trade-off of events, and acknowledging it is part of using them wisely: the decoupling that makes events valuable does make control flow less directly visible, which can complicate debugging and understanding - so you manage this with good practices and by using events only where their benefit justifies the cost. The difficulty is real: because an emitter doesn't reference its listeners, reading the code that fires an event doesn't show you what happens in response - the reactions live in separate places where they subscribed, so to know the full effect of an event you have to find all its listeners, and tracing a bug through an event boundary means figuring out which listener misbehaved. This indirection is the price of decoupling. Several things mitigate it. First, use clear, consistent, well-named events (like 'user.signed_up') so the event's meaning is obvious and searchable - you can search the codebase for who listens to a given event to find all reactions, which is manageable if events are named clearly and you know to look. Second, keep event usage disciplined and intentional - use events where they add real value (independent, one-to-many reactions) rather than everywhere, so the amount of indirection stays proportionate and you're not drowning in hard-to-trace event chains; overusing events is precisely what turns this manageable trade-off into 'event spaghetti' that's genuinely hard to debug. Third, good logging helps enormously - logging when significant events are emitted and handled (as the operational practices covered) gives you visibility into what actually happened at runtime, so even though the static code doesn't show the flow directly, your logs do. Fourth, tooling and convention: keeping listeners organised and documented, or in predictable places, makes finding them easier. There's also a mindset shift: with events you often think less about step-by-step control flow and more about 'what happens in response to this thing occurring', which is a different but workable way to reason about a system once you're used to it. Ultimately, this trade-off is exactly why events aren't for everything: you accept some reduction in direct traceability in exchange for decoupling, and that's a good deal when decoupling genuinely matters (independent reactions, modules to keep separate) but a bad deal when it doesn't (simple direct logic), where you'd lose traceability for no real gain. So the answer to 'doesn't this make debugging harder?' is: yes, somewhat, at the event boundaries - which is a real cost to weigh, managed by naming events clearly (so you can find listeners), using events judiciously (so indirection stays proportionate), logging well (so runtime flow is visible), and keeping listeners organised. Use events where their decoupling benefit outweighs this traceability cost, and prefer direct calls where clear flow matters more - and when you do use events, invest in the clarity and logging that keep them debuggable. It's a considered trade-off, not a free lunch, and using it well means being honest about both sides: powerful decoupling, at the cost of some directness, applied where that trade makes sense.

Session 4 of 6 complete

Next up: Composing Behaviour & Reversibility

Continue to Session 5 โ†’

Related lessons

More in this course

Course home → · Browse all courses →