How I debug — commenting out with binary search
TL:DR when searching for an issue work by cutting the problem area in half and then keep doing this again and again until you find the issue.
The context
Recently I had an issue where I knew there was an issue in my rails_helper file which was stopping my rspec-retry
gem working. There were about 100 lines of config in the rails_helper file.
The problem
How could I find the line which was causing the problem?
Binary search
Binary search is quite a fancy way of saying work in halves.
Commenting things out in halves
What I do is comment out half the file and see if rspec-retry
was still not working. If it was working I know the problem line was in the half I’d commented out.
Below is a diagram of my steps to show you how I went about it.
If after I comment out some lines and it’s still failing I know the problem line is NOT in those commented out lines. Therefore I will expect it to pass when I comment out the other half of the lines.
I continue doing this until I get to my problem line! This is the great point where when you comment and uncomment and comment a single line you go from working to failing to working again. You can be as certain as you can be you’ve found the culprit!
I didn’t know this was called binary search when I started using this technique. It was only when I explained how I was working to another developer they said, “yeah, a binary search”. I had to go google it. Its a nice term but I always just think “cutting in half” whenever I hear it!