When it comes to debugging, we typically have to come up with a number of ideas and try them out. Think of it like shooting from the hip. But there's also a methodical "brute force" technique that we use for isolating errors or performance issues, especially when we're shooting blanks. It's called Delta Debugging.
The Scientific Method
By using this method, you're actually employing the good ol' scientific method. With Delta Debugging, squashing bugs is done scientifically and logically (think more Spock than Kirk). Here's what a scientific method loop would look like:
- This thing is not working as expected;
- If we did this change, we think it'll work as expected;
- We make the change and test it;
- If it doesn't work, go back and try something else
When using the scientific method, what you're trying to do is come up with a hypothesis for why something doesn't work and what will make it work. Then you must prove or disprove your hypothesis. So you test and test and test — then nothing happens. All your theories end up not working out. Or you don't have any ideas to begin with.
Be More Generic
What you'll need is a more generic hypothesis. In other words, instead of guessing the exact solution, you'll want to isolate all the code involved in what you are debugging. Well, that's assuming that the problem is somewhere in that segment of code. And if you've got some 2,000 lines of code, you've probably got some 2,000 potential causes of the issue.
You've got to then remove that code and test to see if the problem persists to ensure the issue is within those 2,000 lines. If it does, then try a different block of code. However, if the problem does not, then take that code and remove the second half of it and retest. Now gear up, because here comes the fun part.
We'll want to continue to remove halves to isolate the exact problem. What you're doing here is refining your hypothesis and working to isolate the smaller segment of code that is wreaking havoc.
Once you've isolated that problematic segment, you can use that information to pinpoint the root cause.
Suited for the Front End
This technique comes in especially handy for front-end performance issues. For instance, we had a page where the scrolling was very slow. We formed a hypothesis that it might have been a problem either with the javascript or the CSS. So we segmented them. Removing the javascript didn't fix it, but removing all the CSS did.
Applying the Delta Debugging technique, we isolated a very large box-shadow that was causing the issue (back when box-shadow support wasn't the best).
Other Real World Applications
Delta Debugging doesn't always have to be applied to code. You can use debugging to also determine what foods are causing you to be gassy. So you might want to start with cheese products. If it persists, then you might want to eliminate milk and ice cream next. If it stops, then you know it's high-time to toss out diary from your diet. All kidding aside, Delta Debugging is simple, yes. Is it always applicable? Of course not. But it's highly effective when it is.