Cam Hashemi

Surgical Refactors

Refactoring is like surgery.

That’s not to say that clean refactors save lives. But messy refactors definitely hurt.

A surgeon is economical with their cuts, and refactoring requires the same mindset.


A naive surgeon takes a quick look at their patient and starts slicing away. They start with a big gash through the patient. They seek to cover as much ground as possible, as quickly as possible. They’re confident that they can close the gap once their work is done. And whenever they see something surprising, they start trying to fix that too, in the same session.

But once all the necessary cuts are made and the patient is ready to get stitched up, that’s when the panic of entropy sets in.

Your patient is now losing blood from every one of those cuts.

The clock is ticking. You don’t know which cut to close up first. Your partner texts you to come home early tonight. You close up one cut, and another one opens. Your coworker asks you whether you have any plans for the weekend. You rush to wrap things up before gravity pulls you away. Your coworkers start to light candles on a birthday cake. It’s your birthday cake. And your patient is bleeding out in front of you.

That all might sound extreme, but maybe only if you’ve never refactored code.

If you step away from all those open cuts for too long, you won’t remember how to stitch them back up again. And with all those cuts still open, you can’t even check whether the ones you did close up worked in the end, because your patient is still laying on the table! At some point, in the chaos of cleaning up after your own mess, you wish you could just start all over again. And thankfully, with code, you can.

But that’s why you’re not a surgeon.


Now imagine how a tenured surgeon works. They don’t just start slicing away! First, they examine. They have a long talk with the patient, followed by an uncomfortably long peek-around, followed by a few technical exams. An X-ray here, a colonoscopy there. Before they even think about picking up a scalpel, they know exactly where they’re going to cut and why.

And on the day of the operation, of course they’ll find surprises. Maybe there’s a huge bad thing that’ll kill the patient in two days if it’s not removed. And I don’t know that much about surgery, but I feel like THEY STILL DON’T REMOVE IT RIGHT AWAY. They use the opportunity to poke around and take notes, then they finish what they went in to do, seal things up, and plan their next move. The patient has to wake up and hear the bad news, but at least they get to wake up. Maybe they’ll even get to have some birthday cake.

The risks of surgery are such that bad surgeons either quickly become good surgeons, or change careers and become programmers.

But programming doesn’t have such immediate risks, so it’s easy for us to keep hacking away. The difference between surgery and software is that in software, we’re the ones who suffer for our mistakes, just not always enough to change our ways.

Hopefully this metaphor makes things a bit more vivid :)