Better Programming

Advice for programmers.

Follow publication

Member-only story

Testing the Untestable —The Battle With Legacy Code

Matthew Lucas
Better Programming
Published in
8 min readFeb 3, 2022
Photo by GR Stocks on Unsplash

Legacy code is a fact of life. So many projects are rife with ancient build tools, abandoned features and most problematic a lack-luster test suite. Often enough, legacy software is the mark of a successful project, one that bears the scars of many hard-won battles. It can also just be a sign of bad practice, an endeavor that wasn’t given enough time or care to grow healthily.

Regardless of which camp it falls into, legacy code is painful to work with. It’s frustrating, it can be risky and a shortage of tests will make you sweat beads of anxiety — how do you know that you haven’t broken something? Do you change as little as possible and cross your fingers that the house of cards doesn’t come crashing down?

First steps back to sanity

When faced with legacy code there are some steps you can take to resume normality. The first move is to find out how the software behaves. That involves reading the documentation and any specifications, right? Well, according to Hyrum’s law, that is probably not the case.

With a sufficient number of users of an API,
it does not matter what you promise in the contract:
all observable behaviors of your system
will be depended on by somebody. — Hyrum’s Law

What the software does is more important than what it is supposed to do. In any system, especially a legacy one, quirks and “features” seep through. Last-minute requirements are squeezed in, never documented and subsequently forgotten. We need to be sure we know what the program does before it can change for the better.

Characterization tests

Early in my career, I had the (dis)pleasure of working on an application previously developed by a “Rock Star” engineer. The product had become key in the company’s lineup but the developer had become disillusioned and went off to play with other toys. The application had zero tests — ZERO TESTS! — and was littered throughout with all kinds of hacks…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Matthew Lucas
Matthew Lucas

Written by Matthew Lucas

Software developer from the UK. Trying my best to make complex ideas simple.

No responses yet

Write a response