Code Management — Refactor This!

During my studies, every assignment I have been handed (with the exception of one) has been started from the very beginning. I was asked to produce an end result by designing, implementing, and testing the software I have written. I have no problems with the Software Development Life Cycle, its actually logical and concise. What starts to bug me is being shoved into a very dirty, unorganized code base and then told that I am not allowed to clean up. Instinctively, my brain figures out more efficient ways of doing things with the code then what is presently in place. But what I’m told is that features have priorities. In the back of my head, I know this is a time-bomb waiting to go off.

So, I move on. Each task that needs to be done has to work with the dirty code, forcing me to jump through hoops. The new code may end up being buggy or having some undesired effect. Lets, for instance, say this new code causes a bug. A fix is issued and it takes 20 minutes to fix. In another portion of the code, something else breaks because it was hooked up wrong. Another 20 minute fix. And so on, and so on. All those 20 minute fixes add up and eventually take up more of my time then it would to refactor the old code into something that is easily extensible and understandable. At this point, the code is twice as big and refactoring would take 2 days. Also, each feature that would take 10 minutes to implement, takes more and more time to implement because the code is convoluted. 10 minutes turns into 15 minutes, 1 hour turns into 3 hours, and so on. Great waste of time, in my humble opinion.

So when is an optimal time to refactor? Would it be at the very beginning when the problem is initially found? Should we wait until the critical features are implemented? Should we just forget about it and keep adding patches until the code is a giant maze of unreadable spaghetti? Why should my job turn into a Dilbert comic?

I honestly don’t know. My first instinct is to give refactoring a try at the very first time I notice a problem. Fix it before it spreads, essentially. Some think otherwise.

Leave a Reply