When a developer is adding code to their project, a common scenario that will occur is a feature needs to expand the functionality of a piece of code beyond the scope of the initial implementation when it was first created. There are two ways to address this.
Hack or Rewrite?
Over a few months, I had hacked a bunch of quick-fixes for a module that permeated across my entire project. It ballooned into nasty code that was a nightmare to maintain and add to. I gritted my teeth, and ultimately decided I needed to perform some open heart code surgery to get to the root of the problem. Two weeks later and presto, all finished! After my rewrite was done, I pondered the ‘why’ of my actions. What affected my decision to rewrite this code, and what were the tradeoffs?
Thus I have determined an outline relating to the issue.
Perhaps due to bad code design, maybe just to have a cleaner syntax, or even performance issues, but a developer needs to write down all the reasons for doing so and the perceived benefits obtained from the rewrite. A big factor to consider - rewriting code can be quite time consuming compared to a quick hack which brings me to my next point.
Quite simply, it is a piece of code that is not a formal part of the design. Often hacks are coded in a quick manner that is (often) sloppy and akin to duct taping something to fix a problem. A good example would be hardcoding variables, instead of passing them through parameters.
How long will it take to rewrite the code vs adding a hack?
How critical is time to you as a developer?
How much time will this save me in the future?
If you need to push out a fix for a problem ASAP, a hack will get the job done while time can be spent getting a formal rewrite finished. Hacks serve as shortcuts in contrast to rewrites which are more time consuming.
How negatively or positively will this affect your codebase?
Hacks tend to bloat the code in a nasty way that makes it hard to read and maintain. In this regard, a developer should seriously aim to minimize using hacks in their code base. Use them sparingly is a good rule of thumb.
Rewrites do just the opposite, it leaves your codebase with a pristine shine and the result is generally easier code use and maintenance.
Is one module affected?
How much of that module?
How big of a module is it?
If multiple modules are affected, then how many and how deep?
If the problem is big enough, a rewrite is often necessary. But if the issue is limited to a few lines of code in a single module, a hack should suffice.
What is the number of developers working on the project?
The more developers, the higher magnitude of issues hacks can cause. If there is a sole developer, he or she will know what the hacks do, and where they are. Add another developer into the mix and the ‘self-awareness’ for the hacks disappears. Now imagine, multiple developers, each with their own unique hacks, that others are not aware of. This will magnify confusion tremendously due to having code that is not a documented part of the design or included as a method. This can snowball into significant problems if not addressed properly!
Hacks generally have a reputation for being quick and dirty. They also excel at testing new things before you invest time into a design that may get scrapped. They are also great for short term fixes, but sacrifice code readability in most cases. This is further amplified by the number of developers working on the same project.
Code rewrites are no angels either! They are time consuming and can cause some serious bugs! Instead of using the old code as hacks do, you are literally tearing out the old code and rebuilding it, while trying to incorporate more features on top of it. If not planned properly, this can break a project. Old code that once worked has been altered (usually) that can cause unexpected behavior if not accounted for.
Both methods serve useful purposes, but with different tradeoffs. Knowing when to apply one over the other can save you lots of development time if done properly.
This is the old code that I was using to broadcast events to objects in my game.
And this is the rewritten new code that has improved functionality!
ABOUT CODESYNDICATE
A 2D spess programmer on a journey (internals not provided) to code paradise.
FOLLOW TIMOTHY
GITHUB PROJECTS