What is Legacy Code?
Legacy Code – a heritage with disadvantages
The word legacy means “inheritance”. Legacy code is therefore “inherited” code programmed by other developers (often a long time ago). Since the further development of other programmers’ source code is nothing unusual in itself, the term is also used to describe
- poorly documented,
- technologically obsolete,
- badly designed and/or
- untested code.
It is therefore a inheritance with numerous disadvantages.
A particular focus on missing tests is made by Michael Feathers in his book Working Effectively with Legacy Code¹:
“Code without tests is bad code. It doesn’t matter how well written it is; it doesn’t matter how pretty or object-oriented or well-encapsulated it is. With tests, we can change the behaviour of our code quickly and verifiably. Without them, we really don’t know if our code is getting better or worse”.
The Clean Code Developer Initiative defines legacy code as source code that “has not been written according to the rules of the art, typically lacks automated tests and does not follow important principles such as DRY – Don’t Repeat Yourself – or SRP – Single Responsibility Principle”.²
How do you recognise legacy code?
Just because a piece of software was programmed a few years ago does not automatically mean its code base is outdated. So how can legacy code – sometimes disparagingly referred to as old code – be recognised? Some indicators help to answer:
- The code is no longer maintained or serviced by the original developers.
- The code was implemented for a discontinued or no longer supported operating system.
- The code was implemented in a programming languages that is now obsolete or for a platform that is no longer in demand.
- The code can no longer be changed and only with great effort. It is difficult to understand, comprehend or extend.
- There are no automated tests and no (up-to-date) documentation for the code.
Clean code and refactoring as answers to legacy code
The main goal of software development is to create useful software with little effort.³ Software should be readable, changeable, extensible and maintainable for other software developers. It is about
- production efficiency and
- continuous improvement.
This foundation of values is the basis for the development of clean code. And clean code is one possible answer to technical debt and legacy code, because it helps in the design, programming and automated testing of new code.
Another answer is refactoring. Refactoring refers to the restructuring of a piece of software while retaining its functionality, with the aim of improving the performance of the software and making it easier to evolve. It helps to improve the readability of the code, extract classes and methods, introduce parameters, reduce dependencies and improve testability. Step by step, the quality of the legacy code is thus increased.
In general, of course, it makes sense
- to conduct code reviews,
- find information about requirements, interfaces, architecture, etc. through document analysis,
- not to make too many changes at once,
- rewrite code only when it makes sense (on the one hand, rewriting is obvious, on the other hand, it can lead to functions being unintentionally overwritten and thus new errors being implemented),
- and to exchange ideas with developers who have previously programmed the code or who have experience in dealing with legacy code.
Impulse to discuss:
If it is difficult to implement software completely independent of its environment, is it even possible to avoid legacy code?
Notes (partly in German):
Interestingly, legacy as an expression far from software development is viewed more positively than negatively. “That’s my legacy”, for example, is an exclamation full of pride in what has been achieved and the heritage left to future generations.
Here you will find additional information from our Smartpedia section: