The Engineer’s Complete Guide to Legacy Code (2024)

Published in

CodeX

·

6 min read

·

Jun 30, 2021

--

The Engineer’s Complete Guide to Legacy Code (3)

We live in a world where progress is king — complete tasks, meet goals, commit more, deploy frequently, ship faster. But what happens when something is lurking in the codebase that’s holding you back? That’s right, legacy code. Let’s take a look at how you can deal with legacy code, once and for all.

In this guide, I will cover the following topics👇

  • What is legacy code?
  • The characteristics of legacy code
  • The problem of legacy code and technical debt
  • Tips and tools for working effectively with legacy code
  • Legacy code migration and cultural shift
  • Resources for dealing with legacy code
The Engineer’s Complete Guide to Legacy Code (4)

When we talk about legacy code, your first thought might be of old, obsolete code. Yes, it may be written for operating systems that are no longer supported. However, it may also mean code which is:

  • Inherited code: it may be from another developer, team, or older software version.
  • Code that the original developer no longer maintains.
  • A code base that is no longer engineered but continually patched.
  • Legacy code can also refer to unsupported operating systems, hardware, and formats.

Ionis characterises legacy code as “the antithesis of clean code which is easy to understand, maintain, and adapt. Legacy code is unwieldy, outdated, and messy, which can lead to numerous problems.” It’s typically resistant to bug finding through automated tests.

Nicolas Carlo calls legacy code the code you’re not comfortable changing but also “valuable code you’re afraid to change” and “the code you need to change, and you struggle to understand.” He suggests that you may, in fact, be responsible for “that code you wrote because you can’t remember the hell you had in mind when you did. Yes, our past self often makes silly mistakes. (If you ever need a good reason for great documentation, this is it!)

Sometimes legacy code exists for a reason. It may point to a team with a crazy high turnover, making little effort in their documentation. Or a team of more experienced developers who are comfortable with older languages. A company may keep legacy code due to competitive advantages over other businesses. They may fear that rewriting code may introduce new bugs or remove hidden functionality.

However, legacy code may also operate in a black box that is less accessible to a broader development community, meaning insufficient quality improvement, security, and maintainability. When people leave, it may be hard to find new developers appropriately skilled to work with it. As time goes on, the code may struggle to work with new and emerging apps and hardware. The company risks falling behind and is drowning in technical debt. Do you hate legacy code?

Ivo Lukač did a survey which revealed over half of the respondents would rather not work on it, and an additional 11% hate it.

The Engineer’s Complete Guide to Legacy Code (5)

Specifically, their pain points are many, with spaghetti code and fear of breaking something the most common concerns followed by the frustration of sticking to depreciated technology. This survey offers an insight into the complexities of dealing with legacy code and explains some of the reasons developers prioritise other work.

Intrinsic to legacy code is technical debt — each time the code changes hands, it needs more and more features and bug fixing, all of which is time-consuming and frustrating, and the amount of technical debt continues to grow.

The Engineer’s Complete Guide to Legacy Code (6)

When working with legacy code, time and energy are precious and you need to know where to concentrate your efforts. Planning and purpose are critical — you need to know what can be changed and what to ignore. Fortunately, there’s plenty of tools available to make life easier:

Static code analysis is the process of debugging by examining source code before a program is run. Code is analyzed against a set (or multiple sets) of coding rules. Then you get a diagnosis report of any violations of these rules based on their severity. This helps you prioritise the most urgent or important tasks.

In general, static code analysis is a good practice even beyond legacy code. It creates the opportunity to identify and eliminate code defects before code is pushed for functional QA, saving a lot of later pain and reducing technical debt. Some examples of static code tools:

A lot of IDEs that are used for code refactoring already use static analysis.

Code refactoring is one way to deal with legacy code. It’s about overhauling code to make it more understandable, maintainable and adaptable through optimizing code, simplifying and merging variables and classes and rewriting command methods.

As Martin Fowler, author of two books on refactoring, explains:

”Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure. It is a disciplined way to clean up code that minimizes the chances of introducing bugs. In essence, when you refactor you are improving the design of the code after it has been written.”

However, you can only begin refactoring once it is clear how the code works — and doesn’t work. The code is then optimized piece by piece. Any unnecessary code is removed or rewritten. Classes and variables in the code are simplified and merged. Command methods are adapted and rewritten. In the end, refactoring is basically a general overhaul of the legacy code. The resulting code is easier to understand, maintain, and adapt.

Developers should test code after refactoring to check for any breaks. Continuous integration is useful as it enables you to revert to a previous build if something goes wrong.

Stepsize VS Code and JetBrains extensions help Engineering teams gain actionable insights into their technical debt, including those caused by legacy code. Engineers can track debt, add bookmarks, and organise their TODOs in their code editor, plus see the impact of technical debt and prioritise it in the webapp.

The Engineer’s Complete Guide to Legacy Code (7)

There’s increased interest in migrating legacy systems to low code development platforms. Sylvain Leroy wrote a succinct but in-depth guide to legacy software migration a few years back that’s worth a read. There are also practices like migrating desktop or on-premise data centres to the cloud and the shift from monoliths to microservices. While discussing all of these in great detail is beyond the scope of this article, it’s important to explore these trends in regard to cultural change and company culture:

  • How do developers collaborate?
  • How is knowledge passed down from developer to developer?
  • What efforts are made to retain knowledge when someone leaves?
  • What are the company standards for documentation and who ensures they are met?
  • How does the company prioritise time spent dealing with technical debt? What incentives do they offer (especially important if everyone hates doing it: maybe they need to provide more time off or bring in specialists?
  • How do you ensure that the new code is clean and error-free from here on? What about training and development?
The Engineer’s Complete Guide to Legacy Code (2024)

FAQs

How to deal with legacy code? ›

How to Work With a Large Legacy Codebase Like a Pro
  1. Be Curious, Don't Be Critical. ...
  2. Don't Code Yet – Use the Platform. ...
  3. Read the Most Important Part of the Codebase. ...
  4. Research the Libraries and Frameworks. ...
  5. Understand the Hardcoded Code. ...
  6. Extend First, and Refactor Slowly. ...
  7. Document Your Journey to Understand a Legacy Codebase.
Mar 20, 2023

Is legacy code code without tests? ›

Michael Feathers introduced a definition of legacy code as code without tests, which reflects the perspective of legacy code being difficult to work with in part due to a lack of automated regression tests. He also defined characterization tests to start putting legacy code under test.

What is the legacy code dilemma? ›

Adding tests: the Legacy Code dilemma

Before you change code, you should have tests in place. But to put tests in place, you have to change code. This is the paradox of Legacy Code!

What are the legacy code challenges? ›

1. Difficulties in Maintenance and Modification. One of the biggest challenges with legacy code is that it can be hard to maintain and modify. This is largely due to the fact that the code might have been written using outdated programming practices or in a programming language that is no longer in widespread use.

What happens if you lose source code? ›

If you've lost the source code, all is not lost. There are options, and it may be far easier and cheaper than it appears. However any such project should not be taken lightly. It will generally need highly skilled and experience people to analyse the problem, determine the options, and then implement the solution.

How to write test cases for legacy code? ›

Start testing from the outside
  1. An Arrange step, where you set up what's necessary to run the test.
  2. An Act step, where you execute the code.
  3. A Printer step, where you generate an output string from the effects of the code.
  4. An Assert step, where you compare the new output with the existing one.

How do you pass a code test? ›

Here are some steps that can assist you:
  1. Read and Understand the Instructions. ...
  2. Analyse the Problem. ...
  3. Plan and Organize the Code. ...
  4. Use Comments and Clean Code. ...
  5. Test the Code Thoroughly. ...
  6. Create a Schedule. ...
  7. Prioritise the Questions. ...
  8. Avoid Spending Too Much Time on a Single Question.
Mar 21, 2023

Is learning no code worth it? ›

Is it worth learning no-code? Yes, it is worth learning no-code if you want to create custom applications without necessarily having the traditional coding skills or the knowledge of a proficient programmer.

Why is it called legacy code? ›

The term Legacy Code usually refers to an application source code that has reached or crossed an end of the support cycle. Often, this may also imply that parts or whole of the application's supported technology stack are either too complex, outdated, or irrelevant to maintain when compared with modern platforms.

What is legacy in programming? ›

A legacy system is any outdated computing system, hardware or software that is still in use. Legacy systems include computer hardware, software applications, file formats and programming languages.

What is codebase in programming? ›

What is a codebase (code base)? A codebase, or code base, is the complete body of source code for a software program, component or system. It includes all the source files needed to compile the software into machine code, including configuration files.

How can I improve my legacy code? ›

Solving Legacy Code Challenges
  1. Review the documentation. The first step in dealing with legacy code is to inform yourself by reading the existing documentation. ...
  2. Divide into modules. ...
  3. Perform code tests. ...
  4. Refactor instead of rewriting. ...
  5. Do a code review. ...
  6. Keep it clean.
Aug 11, 2022

Is legacy code untested code? ›

While these definitions are slightly different, the universal truth about legacy code is that it is code you don't understand. Tested or untested, inherited, obsolete, or whether you wrote it and can't remember why or what it should do, legacy code is an existing code you don't fully understand.

What do you do when you find legacy code in your team product? ›

Solving Legacy Code Challenges
  1. Review the documentation. The first step in dealing with legacy code is to inform yourself by reading the existing documentation. ...
  2. Divide into modules. ...
  3. Perform code tests. ...
  4. Refactor instead of rewriting. ...
  5. Do a code review. ...
  6. Keep it clean.
Aug 11, 2022

How do you deal with legacy? ›

There are two ways of dealing with legacy problems: revolutionary and evolutionary. Revolutionary modernization involves shutting down the old systems and starting from scratch with a new one. Evolutionary modernization modifies the current legacy system step-by-step and focuses on solving specific problems.

How do you deal with dead code? ›

The quickest way to find dead code is to use a good IDE.
  1. Delete unused code and unneeded files.
  2. In the case of an unnecessary class, Inline Class or Collapse Hierarchy can be applied if a subclass or superclass is used.
  3. To remove unneeded parameters, use Remove Parameter.

What happens if someone steals your code? ›

If you discover that someone has stolen your code, you have the right to take action and demand that they stop using it or compensate you for it.

Top Articles
Latest Posts
Article information

Author: Domingo Moore

Last Updated:

Views: 5891

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Domingo Moore

Birthday: 1997-05-20

Address: 6485 Kohler Route, Antonioton, VT 77375-0299

Phone: +3213869077934

Job: Sales Analyst

Hobby: Kayaking, Roller skating, Cabaret, Rugby, Homebrewing, Creative writing, amateur radio

Introduction: My name is Domingo Moore, I am a attractive, gorgeous, funny, jolly, spotless, nice, fantastic person who loves writing and wants to share my knowledge and understanding with you.