Understanding Legacy Code
Published: 2019-08-03 17:27:19

---
title: Understanding Legacy Code
published: true
description: Tips for understanding inherited software systems
tags: 
cover_image: https://cdn.pixabay.com/photo/2012/10/03/22/40/archaeology-59167_960_720.jpg
---

> Definition of _legacy_: something transmitted by or received from an ancestor or predecessor or from the past
> — [Merriam Webster](https://www.merriam-webster.com/dictionary/legacy)


The [law of uphill analysis and downhill invention](https://www.youtube.com/watch?v=nVRUv30coyA&feature=youtu.be&t=1893) suggests that it always feels easier to create something from scratch than to build upon the work of others. However, in regards of software, [the real world consists of brown-field applications](https://www.youtube.com/watch?v=VdI2ca16TFk&feature=youtu.be&t=488), old systems where new parts needs to be added. So understanding Legacy Code — defined here as code where the original authors are not available for questions anymore — is a fundamental part of a software developer's life. This blog post is a collection of hints to cope with this inherently difficult task.


# Have a positive attitude

> Be patient and tough; someday this pain will be useful to you.
> — [Ovid](http://wisdomquotes.com/patience-quotes/)

The same situation may be seen by one person as a burden, while another person sees it as an exiting challenge. It is the attitude which makes the difference.

Still dealing with Legacy Code implies dealing with frustration. So it helps to apply the [robustness principle](https://en.wikipedia.org/wiki/Robustness_principle) not only to code but also to the personal level: "Be conservative in what you do, be liberal in what you accept from others."

Anyway, the more difficult things get, the more we can learn from it.


# Simplify the problem

A model is a simplified representation of the reality which leaves out everything which is not relevant to solve the problem at hand. With this it may be easier to think about the problem and find a solution.

One way to create a model of a software system is to create scratches on scribbling paper or a whiteboard. Find out the names of important things and write them down. Draw circles and rectangles around them. Draw lines and arrows between them. Use different colors.

Also modeling notations like the [Unified Modeling Language (UML)](https://martinfowler.com/books/uml.html), [C4](https://c4model.com/), [Entity Relationship Modeling (ERM)](https://en.wikipedia.org/wiki/Entity%E2%80%93relationship_model), [Data Flow Diagrams](https://en.wikipedia.org/wiki/Data-flow_diagram), etc. can be used. When the sketches are getting complex, it may be useful to use modeling tools like [Visual Paradigm](https://www.visual-paradigm.com/), [Dia](http://dia-installer.de/), or [draw.io](https://www.youtube.com/watch?v=Z0D96ZikMkc).


# Improve the documentation

One of the best ways to learn to explain it to someone else. Further capturing the things which have been hard for you to understand in writing may help you and the people who come after you.

- Look at the [HTML Sanity Checker Architecture Documentation](https://hsc.aim42.org/documentation/hsc_arc42.html) for a good example.
- Create a [README file](https://ymmv.craftswerk.io/2017/10/What-makes-a-good-README) for every sub-project.
- Create a glossary for the project's domain language.


# Be a power user of your text editor

The developers of text editors (incl. IDEs) spend a lot of time and effort to help making code reading efficient. So the investment in learning how to use your favorite editor's advanced features will most likely pay off.

Examples for IntelliJ IDEA:

- [Source code navigation](https://www.jetbrains.com/help/idea/navigating-through-the-source-code.html)
- [Debugging](https://www.jetbrains.com/help/idea/debugging-code.html)
- [Viewing structure and hierarchy](https://www.jetbrains.com/help/idea/viewing-structure-and-hierarchy-of-the-source-code.html)


# Get an overview over the code base

Like for reading books, different [reading techniques](https://www.howtolearn.com/2012/08/different-reading-techniques-and-when-to-use-them/) can be applied for reading code. Set a time box (e.g. an hour) and skim the whole code base. Get a sense for architectural pattern and coding style.

The following strategies might be applied:

- Top down: Look at the API/UI first, then business processes, and use cases.
- Bottom up: Look a the database tables first, then work your way up to the use cases and business processes.
- [Identify the hotspots](https://dev.to/janux_de/hotspot-analysis-for-a-java-project-4ej6), the areas with the highest complexity and most frequent changes.


# Understanding by refactoring

> When tidying we are not here to fix the code, we are here to understand the code. — [Kent Beck](https://twitter.com/kentbeck/status/1120811117515722752)

When the code is hard to understand, it may get easier with an improved code structure. The techniques to get there are known as [Refactoring](https://books.google.de/books?id=o69NtAEACAAJ): Rename variables, Extract Methods, etc. This helps to build a mental model of the application step-by-step.

If the risk is too high to apply the changes in the main code base, they might be discarded after gaining the better understanding.


# Apply static analysis tools

There are probably tools available in your language's ecosystem which make understanding legacy code a bit easier. Here are some ideas for such kind of tools:

- Code search engines, e.g. [Sourcetrail](https://www.sourcetrail.com/) and [Sourcegraph](https://about.sourcegraph.com/).
- Code as city visualizer, e.g. [CodeCharta](https://www.maibornwolff.de/codecharta).
- Write your own utilities with a scripting language, e.g. Python.


# References

- [Software Archeology](https://media.pragprog.com/articles/mar_02_archeology.pdf) | Andy Hunt and Dave Thomas

- [Working effectively with Legacy Code](https://books.google.com/books?id=fB6s_Z6g0gIC) | Michael Feathers

- [5 Tips to Understand Legacy Code](https://www.youtube.com/watch?v=xOsmO4Gnj6c) | Jonathan Boccara
- [Jonathan Boccara on Understanding Legacy Code](https://www.se-radio.net/2019/04/363-jonathan-boccara-on-understanding-legacy-code/) | Software Engineering Radio
- [How To Understand New Code](https://www.youtube.com/watch?v=4e7mjh83hN8) | Dave Xiang
- [Code Reading: The Open Source Perspective](https://books.google.com/books?id=vEN-ckcdtCwC) | Diomidis Spinellis