How To Rebuild Existing Software: Fixing And Updating Legacy Code

・11 min read
How To Rebuild Existing Software: Fixing And Updating Legacy Code

Rebuilding or updating the already existing software may sometimes be a task equally as challenging as building it from scratch. Businesses usually decide to fix their legacy code due to issues with performance or compatibility. On the other hand, developers often are scared to handle older code or code they did not write in the first place.

If you are in such a situation, you better prepare for challenges and problems that will likely occur.

This post aims to explain the nuances connected to legacy code improvements, discuss the most common problems and provide you with best practices and tips on how to successfully go through this process.

What is Legacy Code?

According to Wikipedia, Legacy Code is simply a source code that is related to a no longer supported or manufactured operating system or computer technology. It also relates to code that is inserted into more modern software in order to maintain an older, previously supported feature or file format. On the other hand, the term also refers to executable code that no longer runs on a later version of a system or one that requires a compatibility layer to do so. A great example can be a classic Macintosh application that can no longer run natively on the newest Mac OS X.

The challenges of fixing and updating Legacy Code

Legacy Code is often named ‘spaghetti code’, ‘ball of mud’ or other, not so nice terms. Generally speaking, it usually has one of the following five so-called trouble spots:

  1. It is hard to debug
  2. People do not understand it
  3. The code will not run because the feedback cycles are slow
  4. The automated tests are not adequate or do not exist
  5. Deployment is difficult and/or takes a long time

Img

As mentioned above, Legacy Code usually holds information crucial for the final product to work properly. In some cases, it is code written by people who are no longer in the company. And this is where the problem arises: what has to be changed and where it is? What can we do? The answers are, as follows:

  1. Rewrite it

The most drastic of the possible solutions is to rewrite the code altogether. It might be either the best or the worst way to deal with spaghetti code. It is usually the best option when an infrastructure seems to be impossible to work with and every attempt to fix it causes more harm than good. Here, it is crucial to approach this solution very carefully, which means no matter how good the developers are and how quickly they can finish the job, always assume it will take more time, resources and effort than anticipated.

  1. Work around it

Although it might be tempting to add another layer of the code in order to fix it, most often, is just a temporary fix. What is more, after some time, someone else will probably go back through your code and add even more layers. There is one situation in particular where the work around method is good for - when you are dealing with smaller and centralized issues. In this case, big fixes are not required or necessarily needed by the software. As long as the solution stays well-documented for future development.

  1. Work with it

The final solution is to simply just work with the Legacy Code. Here, the main challenge is to actually understand what the original author had in mind when writing the code. You should be prepared to work on changing one tiny thing at a time, but despite the frustration and possible drawbacks, working with legacy code is a good way to ensure you are not adding the legacy code of tomorrow.

Why is Legacy Code a challenge?

Developers say that the biggest challenge they faced when working with older or unfamiliar code may be the assumptions about it. You may think the code is bad and whoever wrote it did not know what they were doing or that you would have done it better.

Speaking the truth, there usually is a reason why the code you are dealing with is how it is and that is why you cannot put a quick fix on it. There might be some dependencies you are unaware of and that is why it is crucial to know whether to maintain it or change it.

What do you get for doing the dirty job?

By editing the Legacy Code you can simplify everyone’s future workflow. You deliver better code with fewer bugs, which is obviously a win not only for your co-workers but also for your clients. Incrementally improving the code base through step-by-step edits means that everybody involved with the project will have less fear and frustration. By fixing the existing code you make your life easier and prevent the project from future errors that could result from improper code editing.

If you do not feel comfortable or have time to update your inherited codebase, there is always the possibility to outsource it. Our teams can handle this from start to finish while you will be able to redirect your energy to other projects.

Best practices for working with Legacy Code

You obviously will not be able to improve the code overnight, but you can take gradual steps to improve it over time. No matter if you are just getting started or have been working on it for a while, here are some tips and tricks you can follow.

Img

  1. Rewrite only when necessary

Although it might be tempting to rewrite the entire codebase it is usually a mistake. It takes too much time and resources to do so and despite all the effort, it can still introduce new bugs. What is more, you can accidentally remove a hidden functionality.

  1. Try refactoring

It is better to try to refactor the codebase rather than rewrite it. It is also best to do it gradually. Refactoring is the process of changing the structure of the code but without changing its functionality. This helps to clean the code and make it easier to understand. Additionally, it removes potential errors. When refactoring code remember to refactor code that has unit tests, always start with the deepest point of your code, test after refactoring and have a safety net like continuous integration so you can always revert to the previous build.

  1. Test it

One way to get an understanding of the code is to create characterization and unit tests. You can also use a code quality code like a static code analyzer in order to identify potential problems. This will help you understand what the code actually does and reveal potentially problematic areas.

  1. Read the documentation

Reviewing the original documentation with the requirements will give you insight into where the code came from. Having the documentation nearby will help you improve the code without compromising the system since without this information you could accidentally make changes that introduce undesirable behaviour.

  1. Keep the new code clean

Keeping the code clean and readable is a great way to avoid making it even more problematic. By ensuring your new code adheres to best practices you can control the quality of it.

  1. Collaborate with others

As you probably will not know the codebase very well, your coworkers may. It is much faster to ask for help from those who know the code best. Try to collaborate with them as much as possible as a second set of eyes on the code may help you understand it better.

  1. Make changes in different review cycles

Try not to introduce too many changes at once. It is a bad idea to refactor in the same review cycle as functional changes. What is more, it makes it easier to actually perform code reviews as isolated changes are much more obvious to the reviewer than a sea of changes.

  1. Do further research

Working with an inherited codebase gets easier with time. An experienced developer will know when to leave it be as well as learning more about the code itself will. Review sources that speak about introducing changes to Legacy Code, analyze the examples and look for useful tips.

Helpful tools for working with Legacy Code

The developer community seems to have a tool for almost everything you can think of. Working with Legacy Code is no different. When working with an inherited codebase it is crucial to figure out what to change and leave the rest alone. Here’s a list of tools that will help you analyze the code.

One way to get inside the code is by using a code quality tool like a static code analysis tool.

DeepSource

This tool helps you to automatically find and fix issues in the code during code reviews. It can be integrated with GitHub or GitLab accounts. The solution looks for antipatterns, bug risks and performance issues. What is more, it produces and tracks metrics like dependency count or documentation coverage.

DeepSource supports languages like: JavaScript, Python, Ruby, Java as well as Docker, Terraform and SQL.

SonarQube

A popular static analysis tool for continuously inspecting the code quality and security. It is used for automated code review with CI/CD integration and offers quality-management tools. Unfortunately, not every IDE supports SonarQube and you do not have the option to ignore the issues that are intentional or your team decides not to fix them.

SonarQube supports over 25 programming languages including Java, JavaScript, TypeScript, C#.

Codacy

A tool that allows developers to tackle technical debt and improve code quality. It monitors the quality in every commit and PR. Additionally, you can enforce your quality standards and security practices. However, the solution lacks integration of other SaaS services like API QOS metrics from AWS API Gateways or UI/E2E testing Saas services and has a relatively small community.
Codacy supports languages like Elixir, JavaScript, JSON, Ruby, Swift, Kotlin and many more.

DeepScan

DeepScan is a leading static analysis tool created to support JavaScript, TypeScript, React and Vue.js. You can use this tool to seek out feasible runtime errors and quality issues. It can be integrated with GitHub repositories. The small language support can be a major drawback to some while others will be thrilled with that.

Embold

A general-purpose tool that helps to look for critical code issues. This is the tool to investigate, diagnose, transform and sustain applications efficiently. It is integrated with AI and machine learning technologies and can be run both on-premise or within a cloud privately or publicly. On the downside, Embold is quite pricey for what it is in comparison to other similar software.

Embold supports Java, C#, JavaScript, Python, PHP, Kotlin, SQL and others.

Veracode

A tool directed towards security issues as it conducts code checks across the pipeline to find security vulnerabilities. It includes IDE scans, pipeline scans and policy scans as a part of its service. Keep in mind, Veracode does not allow any customization for the scanning rules and has a rather poor UX.

Veracode supports languages including Java, JavaScript, Python, Scala, Ruby on Rails, PHP and many more.

Reshift

A SaaS-based software platform that can be seamlessly integrated into the software development workflow so you can deploy secure software deliverables without slowing down the pipeline. It reduces the costs and time of finding and fixing vulnerabilities, identifying potential risk data breaches and helping businesses achieve compliance and regulatory requirements. The drawback is that Reshift only supports Java.

As additional resources for your journey with Legacy Code, read the article by Michael C. Feathers on how to make changes to the codebase. Another great resource is a book by Martin Fowler Refactoring: Improving the Design of Existing Code with lots of useful tips for effective code refactoring.

Conclusions

As you can see, updating Legacy Code is not a walk in the park. Those updates and their extensivity depend on the age of code, its architecture, test coverage and deployment. Before you start, it is crucial to define the new expectations as well as ensure sufficient test coverage. Remember to always look at the code from all possible angles and decide whether you should work with it, around it or rewrite it altogether.

There are lots of helpful tools that can aid you in every step of the process and I hope that after reading this article, you feel more secure and less scared to deal with fixes and updates.

And if that still sounds like too much work for you, contact us. We will be more than happy to help you. Our experienced teams are ready to start working on your project, even if it still is an idea in your head. Fill out the form to schedule a call and get a free quote.

Related articles