How to make a bug fix way more valuable

2023-09-05

Recently, I was fixing an N+1 issue that made its way into production (hooray Sentry for reporting it!). The actual fix was quite easy, just eager loading a couple relationships. But it annoyed me that I didn't find this in testing. I did have lazy loading protection enabled, and I had test coverage of the code, but the setup portion of the test didn't create multiple related records, so the tests couldn't catch the N+1 problem.

But before I added the one-line eager loading fix, I updated my test so that it triggered the lazy loading protection. Not only did this give me verification once I fixed the problem, but it made sure I wouldn't break it in the future. The earlier you find bugs, the more stable your app is. I'd rather find it during local development than in production. And yes, I consider poor performing code (like N+1 problems) to be a bug.

If you're fixing a bug today, can you take an extra few minutes and write a failing test before fixing the bug? Do this enough times, and your app becomes very resilient to regressions.

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.