Deleting stuff is fun and helpful!July 06, 2022 #debugging
Debugging problems during software development can be frustrating, even if it's immensely rewarding once you eventually figure it out. This is especially true when reproducing the problem seemingly requires some complex dance through tons of other parts of the project. Frustration is further exacerbated when the language used is one you're still trying to master.
There's one technique I've learned through the School of Hard Knocks to deal with problems that are overly complex in one form or another: simplify it
What do I mean? I mean start removing / commenting out code that is seemingly unrelated. Try to simplify the input into the function or whatever into the simplest form that causes the problem to happen. Pull out portions of code that you think are related to the problem and build a new program in an attempt to reproduce the problem with some skeleton of the original code base / configuration. With source control, you're not really at risk of "losing" anything, so go wild with removing things. Your goal is to recreate the problem at hand with as simple of a program (and input!) as possible.
Why would you want to do this? I'm glad you asked!
Easier to spot the root cause
Oftentimes in the process of simplifying it, I notice the cause of the problem. "Oops, I'm pretty sure I didn't actually mean to do that." - me, a lot.
Easier for comparisons
If the problem magically goes away after simplifying it, then I have a valuable starting point for diffing the broken program against. If it's still not obvious, then I start re-adding components (with attempts to reproduce in between additions) until I can cause the problem to happen again.
Easier to ask for help
If I simplify it and the problem still happens, it's much easier to share a simplified version when asking for help. Somewhat unsurprisingly, people are generally more willing to help if they don't have to spend half a day studying the entire codebase to get a handle on how things are supposed to work. Allowing them to also reproduce the problem more easily can only help.
Easier to turn into a test
Simplifying the code to reproduce the problem can include removing unrelated functionality and trying to mock out unrelated dependencies. This is like... 90-something percent of the way towards writing a test. So once the issue is discovered and resolved, I already have a test ready to go that can confirm the fix and help guard against it from happening again in the future! Win!
The next time you're baffled while debugging by some weird behavior, just start deleting stuff! It's fun! And it'll probably help!