(This was episode #11 of my Event Modeling & Event Sourcing newsletter)
This weekās edition is arriving a little later than expected, but I hope itās worth the wait! Today, weāll dive into the topic of code reuse and why, contrary to popular belief, itās often not the best idea.
The discussion began with a common question: āShould we reuse simple classes like DTOs across the system?ā
If you had asked me this 10 years ago, my answer would have been an enthusiastic āYes! Keep it DRY. Reuse is good!ā But today, my perspective has shifted. Let me explain.
Why Code Reuse Is Tricky
For many of us, the principle of reusing code is ingrainedāperhaps from university, where itās still taught as a best practice. So, when we encounter simple value classes like Email, Address, or Location, it feels natural to think: Why not reuse them? They rarely change, right?
Well, hereās my answer: Donāt, if there is not a very good reason.
I know this sounds controversial. In fact, during the discussion, someone quickly raised the usual counterargument:
āIf we donāt reuse classes, wonāt we end up with mapping chaos and duplicate code everywhere?ā
A Real-Life Story
To illustrate my point, hereās an excerpt from chapter 10 of my book, Understanding Event Sourcing. Itās based on one of my first projectsāa classic example of how reusing value classes can backfire.
Before you dive into this topic - there is a new Online Course āImplementing Eventsourcingā, that will guide you step by step through this process. Find more details at the end of this article.
Imagine youāre faced with a choice:
- Create a central location for these reusable classes and share them across the project (from domain to presentation).
- Reimplement them in each module, risking what I call Mapping Hellāmost of your code ends up translating from one context to another.
Which is the smaller problem?
The Problem with Reuse
Letās take a Location class as an example. It contains latitude and longitudeāseems simple enough to reuse, right? Not so fast.
By reusing the Location class, youāre assuming that its definition is consistent across the entire system. For some modules, this might hold true. But in others, ālocationā might represent something completely different, like a physical address.
Now consider an Address class. At first, it might seem reusable too. But what happens when one module needs to add a companyName field to support business addresses? Suddenly, every module in the system inherits this new fieldāeven those that donāt care about it. This creates strong coupling between modules.
The biggest issue? Where do you draw the line? Is Location reusable but not Address? How do you decideāon gut feeling?
My Approach: No Coupling Between Modules
My approach is simple: Prevent coupling at all costs. Code reuse or DRY is not my priority.
So, I never reuse value classes across slice- or module-contexts. Ever. It might seem extreme, but this clear guideline ensures that vertical slices remain independent. And the freedom this approach brings is worth it.
But What About Mapping Hell?
Yes, at some point, you need to translate between contexts. But if youāre working with Event Sourcing, youāre already projecting data from your event store to your target models. There are no global assumptionsāyou simply build the models you need.
In this sense, CQRS complements Event Sourcing beautifully. You store data one way but read it in multiple ways. If you find yourself constantly mapping the same information between layers, itās worth revisiting your system design. Mapping should not dominate your codebase.
Food for Thought
As always, this email reflects how I personally approach a common challenge. My solution may not be the best for everyone, but it offers a perspective that I hope you find thought-provoking. Itās the kind of discussion thatās best enjoyed over a few beers.
Want to see this in action?
Join the free newsletter to get Event Modeling & Event Sourcing once per week to your inbox.
If this topic resonates with you, there is a full recording of my Webinar: Full Circle: Plan, Build, and Verify
Weāll cover the entire processāfrom requirements to prototype to codeāin just 90 minutes. Youāll see exactly how I work, and the Q&A at the end will be a perfect opportunity to ask your questions.
Want to practice your skills in a safe environment?
The Online Course āImplementing Eventsourcingā gives you exactly that. Itās a perfect companion to the Book āUnderstanding Eventsourcingā!
Everything I know about Evenet Modeling & Event Sourcing in one course.
Martin Dilger
Join the Agentic Engineer Program
Apply Spec-Driven Development Hands-On - Event Modeling, Event Sourcing, and AI Engineering with autonomous agents.
Book a Call Today
Want to talk through how Event Modeling could work for your team or project? Letās have a quick, no-pressure conversation.
