How do you test code you don't understand?

And without tests, how could you ever change it?

Joel Clermont
Joel Clermont
2024-05-31

If a project has lived a long time, there is a good chance that lurking somewhere in that app is a section of code that no one fully understands anymore.

Maybe the code is messy and untested. Perhaps the original developer is no longer on the team. Or maybe there is just so much logic that it's hard to keep it all in your head.

We inherited a project that hit all three of these points. Even worse, this code was the "crown jewel" of the app, central to the main feature that people paid for.

We wanted to start some very basic refactoring of the code, breaking it down into more understandable chunks, and documenting it along the way. But how could we be sure we weren't breaking anything in the process?

I experimented with a technique called "differential fuzz testing". It's a fancy name for a simple concept: run the original code and the refactored code side by side, and make sure the outputs are the same.

To help me do this, I used the Eris testing package, which works on top of PHPUnit.

It allows you to set up a large sample of generated inputs, following rules I specify, and feed those into both versions of my big calculation class.

Even better, it has smarts built in to help with picking random values in a repeatable way and shrinking the input set when it finds a failure.

This technique worked beautifully! I can now run hundreds of thousands of iterations any time I make a change, and have confidence that no behavior changed.

This is a very specific tool for a very specific problem, but it's good to know it exists, so you can reach for it when it makes sense.

Here to help,

Joel

P.S. I recently did a live demo of this technique as part of our Mastering Laravel community. Once you join, you'll get access to the recording.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼

Level up your Laravel skills!

Each 2-minute email has real-world advice you can use.