Anecdote: Building on a Bug

I spent much of the past few days fixing a bug in some database access code that I wrote last year. Up until now, that code had caused very few problems, and I was quite proud of it. (I still am, in spite of this bug.)

In this case, I was losing database records that linked parent entities to children, but only in a circumstance where an entity with a list of child entities was created, saved, loaded, modified and then saved again. The lazy loading prevented the child entities from loading, as it was supposed to, but upon save, I was erasing all linking records, and then because the children weren’t loaded, skipping the writing part.

Once I identified the problem, I wrote a failing test case, fixed the original code, and confirmed the test passed. But here’s the rub: two new tests began to fail! After questioning whether I really had the correct solution to the original bug, and I wasn’t hallucinating, I began to investigate these failures. It was then that I discovered what had been happening: my code was *depending* on that bug. I had to fix that code too.

Oops.

Leave a Reply

Your email address will not be published. Required fields are marked *