Good engineers constantly learn new things, grow their knowledge, and gain experience. This however sometimes creates the temptation to change things; perhaps you found a better approach while working through a new feature and realize that the same approach could greatly simplify existing systems. Maybe you learned about a new framework feature or a new library and now you’re itching to integrate it.
Whatever the case may be, there’s always a trade-off to be made; Do I spend my time refactoring, or do I just accept things how they are and move forward? This is a surprisingly difficult decision at times, but the trap is in not thinking before committing.
Endless Refactoring Trap
The greatest risk is getting caught in the trap of endless refactoring. What may start off as a “small change” can easily ripple through an entire system. Every change has a cost; invalidating tests, introducing bugs, breaking integrations, disrupting downstream users, and of course pushing back delivery dates. The engineer must make a judgement call; does this change add enough value to justify the potential costs?
Sometimes the answer is absolutely not. Legacy systems often exist for decades because they successfully solved a problem, regardless of how terrible the code may be. However, a significant amount of time and cost is often spent modernizing legacy systems because they no longer support the changing business needs, whatever they may be.
If a golden rule of thumb exists to reference when making a decision to refactor a codebase, it’s arguably centered around the cost/value trade-off, but there is no universal metrics for this; it’s entirely based on the business, priorities, and situation. This is where judgement and experience really pay off.
Even once you’ve made the call and decided to move forward, you still may find yourself needing to sell this decision to the shareholders. What an engineer may value isn’t always what the business values. You may see a shiny golden path ahead, but the people paying your check may not be convinced the time and cost is worth it. Even when you are the business owner, you sometimes need to switch from an engineering hat to a business owner hat.
Experience Changes Perspective
There are endless ways to structure code, each with their own trade-offs, pros, and cons. Blitzing through without thinking will almost certainly come back to bite you later. However, there are plenty of times when you did put in the engineering work, you thought through the problem carefully, and what at the time felt like a solid design, later makes you question what the hell you were thinking.
With each new thing you work on and each new problem you solve, your understanding and perspective change. The way you think about systems evolves. Perhaps inverting relationships between entities, or the abstraction you once thought was great begins to reveal it’s flaws. However, the question you need to ask yourself is, what friction does the existing system create? What problems actually need to be solved?
Maybe I’m speaking for an audience of 1, but to me, architecture and software design is a fun, rewarding activity. I have to constantly question my motivation behind every decision I make; is it purely aesthetic friction? Are there just minor inconveniences? Do I just feel it _could be better without actually having an objective reasons behind it?
Deciding The Value
I argue that value stems from reducing friction. If the code is fighting you and each change comes with an overhead that slows your productivity down, to me that’s a pretty good indicator that there is enough value in taking the time to refactor. I won’t sit here and try to create a universal metric or decision framework. At the end of the day, I can only speak from my own experience.
Priorities will be different for each person, and each situation. My personal priority is almost always about the downstream cost of change. Because of the fact that my current focus is primarily in game development, the cost of change is something I’m especially sensitive to. Game design is subject to constant change, which means the systems supporting it are also subject to constant change. There are times when deleting an entire branch of your code is the right call, and other times when changes to core mechanics force you to reconsider your approach.
Ironically perhaps, I also optimize for velocity, but unlike the modern tech industry, my strategy is to spend more time now to increase velocity later. This very frequently is a huge win for me because my velocity isn’t constantly slowed by friction in my code.
Change Is Inevitable
I originally wrote Refactoring Is Inevitable for this section, but in reality, the need to refactor is almost always due to changing requirements. I had originally planned to talk about eliminating the need to refactor in the first place, but that’s both naive and damn near impossible. The most experienced engineer and systems architect in the world can not predict every future decision, and trying to account for every future decision is what leads to sprawling, bloated, over-engineered code bases that nobody wants to touch. In fact, one thing I’ve argued before is that over-engineering can increase human as well as AI costs.
However, there are ways to reduce the amount and cost of refactoring. The trick is to reduce the Blast Radius. This article does a pretty good job of describing what I’m talking about, but is focused more on system failures rather than architectural ones. A few things it does touche on includes:
- Service Boundaries
- Service Isolation
At the risk of sounding like a reductionist, I think this can be summarized as “keep thing small”. Keep domains tightly scoped, minimize abstractions leaks, and create clear communication channels. Of course the challenge lays in finding the boundaries, preventing dependency cycles, and exposing enough information without forcing external systems from needing to know about the implementation details of another. This can be especially challenging in game development where systems are highly interconnected.
The Perfect Design Illusion
A perfect design doesn’t exist. A good design reduces friction. A bad design gets in your way and forces you to make compromises. A great design reduces friction, survives change, and reduces cognitive load. A great design in my experience, starts as a good design and evolves over time.
In the 90’s the Waterfall Model was predominate in the tech industry. The idea was that everything would be designed and planned out ahead of time, leaving the engineers with the simple task of implementing the code. This model assumed that the design was fixed, or at least wouldn’t change that much. The reality was, things change. Requirements shift. The market evolves, and your competitors are constantly forcing you to add new features.
We’re seeing the same trend happen in the modern tech industry due to AI. The huge waterfall design documents are now being written as specifications for agents to execute. Only now, instead of a team of engineers carrying out the implementation, the task has been delegated to an LLM. However, the problem of change hasn’t magically disappeared, and if anything, the risk of getting the design wrong is even higher than ever when the friction feedback loop no longer becomes visible until much later.
The point of this section is really to say that when deciding if changes to your codebase is worth the time, first realize that your goal isn’t to create the prefect design. If it can survive change, and the friction is manageable, maybe consider moving on instead.
Conclusion
I recently wrote several refactor tasks. Within the details for each, I added the same section: What value does this provide? This forced me to stop and really think, but revealed a hidden benefit; realizing that the changes I was proposing would actually make it significantly easier to integrate the new features I had been planning.
The other question I asked in each task was “Can this be simplified?”. It’s easy to get caught up in the details and fail to see the bigger picture. Thinking through how something could be simplified forces you to stop and think holistically.
Refactoring code can feel productive in the same way buying a domain for a product you’ll never ship can feel productive, but before you take out your credit card, you should first stop and consider what value you’re actually gaining.