I think every coder and every company that's been around long enough arrives at a situation where for whatever reason, old code needs to be re-used. But not in a, 'Hey, I'll grab this function from the site,' way but in a, 'Hey, we need to redo the front end for this site without redesigning the code base,' way. But seemingly without fail, this code was created three versions of coding language ago, using barely logical, much less programmatically sound, methods. As a coder, how do you avoid pulling out all your hair in an instance like this?
There could be a number of reasons for doing this. One, there may not be enough budget to redesign the code. The idea is that by not spending the time necessary to document and and then reprogram the code base, time is saved because the code, in theory, already works. Two, if no one really knows how the code works and a deadline is approaching, it may be hard to justify spending lead time on the process necessary to document code that seems to work. However, I might argue that when making a decision like this, one should consider the support time that might be required to ensure that everything is working properly once the site goes live, the time cost of combining two or more disparate types of code which may not want to 'play nice' with each other, and other similar factors.
Ultimately, whether or not to re-use or re-appropriate legacy code depends on the budget, time constraints, and other business factors. If the person who designed the code is available, then this process is made easier; if not, the person who picks up the project has to make some tough decisions. If the decision is made to use legacy code, a developer's best plan is to:
- Be organized. Keep notes on everything and refer to those notes frequently. The code may be unorganized, but that doesn't mean you have to be.
- Have a plan and follow it throughout your development. If you have a certain way of formatting code, make sure you do it everywhere. Come up with way to address general problems you will encounter. If a function doesn't work, are you going to rewrite it from scratch? Are you going to attempt to fix the code in-place? Whatever you decide, apply the same thing everywhere.
- Be patient. Dealing with legacy code can be frustrating and slow. Take it one page / function at a time. Speed is always important, but never at the expense of good code.
- Keep perspective. Yes, you will encounter crazy, illogical ways of doing things. You will not believe that someone could think this way. However, the point is not to let that distract you from the goal of creating good code. Create a 'Wall of Fail' like we have where we poke fun at these things.
So if you are presented with legacy code that needs to be re-appropriated, try to follow these steps. If you do, hopefully at the end of the project you will be able to don a smoking jacket and not a straitjacket.