- refactor
- engineering
A quiet refactor that paid for itself in a week
A small, unloved helper class had grown into the thing everyone reached for. Replacing it took four days. The metrics showed up before the release notes did.
This is a small story about a small refactor. I am writing it down because the most useful engineering I have done lately has been this shape, and I never see it written about. Blogs are for the dramatic stuff. Most of the work is not dramatic.
The smell
There was a helper object on one of our request paths. It started life, years before I arrived, as a tidy little thing that held the three or four values a request handler needed. A request would come in, we would build one, pass it along, and throw it away at the end. Fine.
By the time I was looking at it, it had grown to more than two dozen fields. Some were what the name promised. Others were expensive things that only a minority of requests ever used, set up eagerly on every single one.
I did not go looking for this. I was chasing an unrelated latency regression. The profiler kept pointing me at the place this object was created, which was doing far more work than its name suggested. The name said "context". The body said: initialise, resolve, allocate, and carry a decade of accreted decisions.
That is the first smell. When something named for one thing has to do six things to exist, the code is telling you something.
The measurement that made the decision defensible
Before I touched it, I wanted a number I could point to. You cannot refactor an important piece of code on vibes.
I added a little instrumentation and let it run for a couple of days in a test environment. The answer was clear: most requests were paying setup costs for work they never did. Not an emergency. A tax. Stack enough taxes and your tail latency starts to drift.
I wrote a two-paragraph doc. Here is the cost. Here is the shape I want instead. Here is what I am not proposing to change yet, and why. I sent it to two engineers I trust, one of whom knew this code better than anyone, and asked them to poke at it.
They both pointed out the same thing, which is why you show work to people who know it better than you do. One of the fields had an undocumented contract with another part of the system. A clean refactor that ignored it would have been a correctness regression, not a performance win.
The change I actually shipped
The change that landed was smaller than the first one I drafted. I had wanted to split the object into five small, focused pieces. That would have been the textbook answer. It would also have been a three-week project touching every handler.
Instead I kept the outside shape exactly the same, so no caller had to change. Inside it, the expensive parts became lazy, so only the requests that needed them paid for them. The field with the hidden contract stayed exactly where it was, with a comment explaining why, because I had just learned it the hard way and did not want the next person to.
A handful of files. Two review rounds, both useful, neither painful.
I want to call out the decision not to split it. Every instinct I had said I should. But the change I shipped addressed the measured problem and left the rest alone. The split could be a future project, if it ever became worth doing. Most of the time, it will not.
What the metrics showed in a week
I rolled it out gradually behind a flag and watched each step.
Within a week the setup cost was a fraction of what it had been, and the latency on the path I had originally been investigating improved more than this change alone could explain. That meant there was a second cause I had not found yet. The fix had made it visible, which was part of the point.
Memory pressure dropped too, which I had not predicted. The reviewer who cared most about garbage collection noticed before I did, which was pleasant.
Nobody wrote a release note. It was a chore commit with a short message. The next week, I went after the second cause, which turned out to be in a completely different layer.
What I take from it
A few things, none of them surprising.
Measure before you refactor. Not because I did not trust my reading of the code, but because the number let me explain the decision to someone who had not read it. A short doc with a measured cost in it is harder to argue with than an opinion.
The refactor I shipped was smaller than the one I wanted to ship. That gap is where most of my growth has been lately. Smaller was the right call, even though it left visible mess behind. Visible mess is not the enemy. Invisible mess is.
And the thing I keep relearning: the moment you add a measurement, you make a second problem findable. Almost every small refactor I have done in the last two years has surfaced a neighbouring issue I did not know existed. That is not the refactor failing. That is the refactor doing its second job, which is to clear enough undergrowth that the next thing becomes visible.
This one paid for itself in a week. Most do not pay back that fast, and I am fine with that. The discipline is to keep making small, measurable, boring improvements, one line at a time, without congratulating yourself for it.