Refactor or Hack

In my day to day, I often come across code that was written before I arrived. Sometimes it’s great. Easy to understand, abstracted at an appropriate level, maybe even commented (gasp!). Sometimes, though, it’s been written poorly. If the person is still with the company, I can just ask about their intentions or for clarity if something is easily followed. The worst case is when the code is written poorly and by someone who is no longer with the company. Now I have code that is hard to understand, the person that wrote it is gone, and I’m being asked to either fix or implement a new feature utilizing pieces of this existing code.

As I dig further in to understand what’s happened before me, I realize that there are a multitude of issues with the existing code. It didn’t account for this case or that business rule that it was supposed to. It’s duplicating data in two tables or it’s written in an unintelligible way in order to be “fancy”. I like fancy code as much as the next developer, but more often than not, simplicity is best. Me: 0. Technical Debt: 1.

This brings me to the all-important question: Refactor or hack? Should I take the time to refactor the existing code, clean it up, and make it easier for the next person that comes along or should I hack at the existing code to deliver the feature or bug fix on time and on budget? It’s difficult for a developer to answer this question because most of us want to do a good job and create the best possible code base even if it takes longer. We take pride in our work and genuinely want to do a good job. Often the pressure from the business side reveals that they don’t care how well the code works under the covers. They just want it to work and work today.

I’m fortunate to have a great business team that understands the long game and genuinely wants an easily maintainable code base, but I still struggle with “how long has this story been kicked down the sprint road because of other broken places in the code?” I don’t have an answer for this question at the moment. It’s just a thought that’s been tumbling around in my mind as I’ve added at least three stories to refactor large portions of code in an effort to simplify and make go-forward maintenance easier.