As a developer, I hate working on legacy code and software, it's exhausting, the code is badly formatted and all over the place, typically, it'll take a lot longer than needed to even make a code change, even if it's a small system.
Working with legacy code can be a chore, but, since we don't throw away software every few months, it's a chore that's inevitable and has to be done by someone right?
That someone might be you.
This is why, I've put this article together to give you the 5 best tips for working with legacy code and software that'll hopefully make development a little easier.
Legacy code and software is the source code of a project or web application that's inherited from someone else from an older piece of that software. It's code that you don't really understand all too well and can't be changed easily.
It's built like building a house that you weren't involved in and now you've got to finish building the house since the contractor left, where do you start?
You could start by headbutting the wall of what's already been built, but that isn't going to get you very far at all.
Instead, you could just read the tips that I'm going to give you, and that will be the best starting point.
As soon as a piece of code is written, and has been integrated and deployed to a production environment and used by whoever the intended users are, you could essentially refer to that code that was written, maybe just a few hours ago as being legacy code.
Especially if you're planning on rewriting it soon after getting an initial version of your feature finished.
Here's the thing, if we spent weeks upon weeks refining how a system works and ensuring every possible piece of functionality has been baked in, it would never go live.
As a business, the business would be spending weeks of your time developing a feature, that's so robust that in the end...
Never actually works as intended or underperforms.
Instead, it's best to get a MVP (Minimum Viable Product) out to market with the code that you've worked on, and then rewrite it once you've verified that your new feature works.
Often though, developers are forced to continue working on that MVP that it becomes the norm.
It sounds so simple doesn't it, yet can take a long time, but sometimes it's worth it.
You need to be testing the code that you're working on, even if it's legacy code, it might not be working as the code owner initially once intended many years ago.
Testing is a critical part of any development process, whether that be when installing a boiler into your home, or building a bridge.
Tests are everywhere.
Sometimes it's a good idea to manually test the code to figure out the intended behaviour, and then consider setting up unit tests which will automatically test that piece of code each and every time you come to merge your changes to ensure that it's always working as intended.
Ensuring that tests are set up means that as the code becomes older and potentially more fragile, you'll always know exactly whether the code is working or not.
Developers come and go, software is released and taken down, and ultimately, along the way, there should be some level of documentation to go alongside whatever has been written.
Sometimes, there isn't, and it can be an afterthought since everything the developer of the software has written is all locked away in his head.
Documenting how a system works, such as a web API is crucial...
All of these are points that need to be documented, and the best place to do that as a developer is going to be the project's "readme" file as it allows for markdown for fancy code blocks and formatting.
Working with legacy code absolutely is going to require this type of documentation, so if you're inheriting some legacy code, it might be best to keep a log of what things do so that you're not confusing yourself or others.
I love the word "refactor", I'm not so keen on the word "rewrite" though, are you?
Do you know what the difference is?
Let me tell you...
It's often asked which one will be faster to complete by project managers, but speed might actually negatively affect how each of the methods work.
If a particular method technically takes less time, it may not be justifiable to even go down that route because it too will end up becoming legacy code more quickly.
I'd say in some circumstances, you're probably better rewriting a piece of code, but it can also make total sense to simply refactor too.
When working with legacy code, it's often all too easy to fall into the habit of writing legacy code as much as it is to work with it.
This can often dig a hole deeper beneath you when you're loading up that new function that you've just written with hundreds of lines of code just because the legacy code you're working on has done the same.
If you're working with legacy code and software, try to at least implement the DRY principle wherever possible.
What does this mean?
Well, it means to not repeat yourself, and that can sometimes be difficult to do when working with older code.
Since the time of the legacy code being written, chances are, if it's been a few months at the very least, there's going to be a new way of approaching a problem.
This might mean that the methods currently implemented into the legacy code can be swapped out with some helper functions for common functionality instead.
The only way you're going to figure this one out, is by taking some time to do some research, and that might be code research that you dedicate a specific amount of time for, or, you might choose to do it whilst you're working on refactoring some legacy code instead.
Whichever approach suits you, go with that.
I hope you enjoyed this article, I'm sure over time I will be back to add more great tips into here since working with Legacy code can be exhausting sometimes.
If you enjoyed this article though, feel free to give it a share and don't forget to take a look at some of our other articles too.
Notification Contacts make alerting team members incredibly easy and simple, find out more about this new feature that we've released for all of our customers!Read more
Here at the Domain Monitor, we're delighted to announce that Pretty Purchases, a UK based E-Commerce website that specialises in selling handbags has joined our website monitoring tool, learn more and find out how you can get a 5% discount today!Read more
If you've ever received a notification from our website uptime monitoring tool, you'll know how scary it can be to find out your website is down, your stomach literally drops and you start to panic, is your website actually down? Here's what to do when your website goes down.Read more
Plus, more great content, product news and updates