3 Ways to Deal with Legacy Code

William Wu
Nov 1 · 3 min read

Before I begin this article today, a question comes to mind: how exactly do we define legacy code? The definition from Technopedia is code that is no longer supported or in active development/engineering but simply being patched over time. It could also be code that is still supporting old and unsupported systems and hardware.

One of my definitions of legacy code however, is code that no one or only one person in the office knows what the code does, or no one online can answer whatever question you may have about it.

Well in that situation, what do we do about it? Here’s 4 ways I’ve discovered while working with legacy code.

Just `grep` the source code

The unfortunate truth is that Stackoverflow probably won’t help too much with whatever problems you’re facing with the legacy code or system because there’s also a high likelihood you’re working with a custom framework. Nevertheless, a lucky (if you can call it luck) aspect of working with source code is the ability to search through the codebase. Unlike operating systems where it becomes difficult to look deep down into, code allows you to see what others may have done in similar situations.

My methodology when looking through the code base is to start with error messages or GUI element in an around the problem you’re looking at. Starting with the entire message, you might get lucky and find the exact line of code that prints out the error message. However in most cases you won’t in which case you’ll need to gradually reduce your search string till you’re left with a few key words.

You might get a line of code with no explanations, only if you’re lucky. So just try it*

*As long as this is NOT on a live PRODUCTION environment

Sometimes even though the answer you’re looking for doesn’t exist on Stackoverflow it may exist on a forum somewhere probably in another language. Just a single line of code with no explanations.

For myself, this occurred when I was developing some code to interact with the windows kernel. I searched everywhere to figure out whether it was possible to pull out some information while executing code within the windows filter driver. Some places online mentioned that it was indeed possible, however never mentioned how.

After frantic searching, I found a few lines of code and a comment saying. This worked — with no explanation of how it worked. Needless to say the only way forward then was to try it out.

A few blue screens and crashed VMs later, the code was working. However it just goes to show how difficult it is working with legacy systems. Even then, significant testing was needed to demonstrate no adverse side effect occur while using the code.

Don’t assume the code is perfect. But do try to preserve the original behaviour

One of most difficult aspect I believe when dealing with legacy code is the balance in emotions — one on hand there’s the want of just fixing whatever the problem is, and on the other hand an intense desire to destroy the entire codebase and rewrite it from scratch. In that balance, the most important issue is the preservation of behaviour.

This may sound simple however, if there is no preexisting tests or documents it become difficult to really check whether whatever code is written has any other side effects besides fixing the bug.Nevertheless, the only way around this issue is to thoroughly test the entire product.

It always takes longer than you think to grasp whatever the code is doing.

I believe at the end of the day whatever issue it is that you’re working on, it will probably take you longer than you think. That’s not to say this is only an issue in legacy systems, however it is just more prevalent when working with legacy systems. Try to allocate at least 20% more time and simply try not to be too hard on yourself.

One big lesson I’ve learned over the years working with different languages and software frameworks is that it will always take a while to get into the flow of things so don’t be too hard on yourself. It’ll work out.

Software Developer/Trader — Java, C++, VHDL, etc. <br>

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade