How I debug — commenting out with binary search

Kevin McCarthy
3 min readMar 22, 2021

--

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!

nothing commented out and it fails
we comment out the top half and its still failing. We know the problem is not in the top half of the file.
we comment out the bottom half and we find its working. This means the problem is in one of the commented out lines.
We uncomment the top half of the commented out lines and discover it’s still working. So the issue is within this bottom quarter of the file.
We uncomment a few lines above the bottom and discover its failing. This means the problem is not within the bottom bit of the file.
We comment out the one line left and discover things are working.
We’ve done it, we’ve identified the problem line.

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!

--

--

No responses yet