I had a working Find command, but needed to factor the code so that I also had Find again. One has a UI, the other just repeats the previously specified search. The code that does the finding should be the same in either case. Splitting off common code is called factoring.
After factoring, I tested the Find command. It works.
Getting ready to test Find again, I paused, thought to myself — there’s a chance it might work the first time. Bad idea. Of course it didn’t. No big deal, I’ll just step through the code, watch it fail, and figure out what needs fixing.
I notice a value is undefined. Weird. I repeat the experiment and watch it get its value. It’s calling a routine that’s been called millions of times. It works as well as anything. I seriously doubt it has any bugs. But there it is returning an undefined value.
It never occurred to me to check if the thing I’m asking for actually is undefined.
Two hours later it finally dawned on me, that was the problem. I had been assuming undefined was the wrong answer. It was the right answer. There was an edge case which I was hitting now, which had nothing to do with the Find command, that was causing the value to be undefined. I added a bit of code that checks for the condition, and fixes it, and voila, it all works.
Moral of the story, the bug was available to be discovered within minutes, had I just asked the right question.