Treat the illness, not the symptoms

Jason Gong
2 min readNov 17, 2021

So this week I have been on the bug hunt and the topic of the correct approach to bug hunting has come up in the my morning standup. As a developer, it is very instinctual to want to address the bug immediately and prevent users from experiencing an error screen. But there is a fault in this logic if addressed in the wrong way. Let me show you an example.

NoMethodErrorcart_items#destroy undefined method `destroy’ for nil:NilClass

The above is an error when the user was attempting to remove something from their cart when the item was already removed. We have the option of identifying and fixing the bug. This approach is much less straightforward and can be very time consuming. Or we can head off the bug with a conditional and preventing the code from erroring.

@cart_item.destroy if @cart_item.present?

There are basically two ways to approach a bug. Find, identify, and then either fix the bug, or triage the bug. You can see above how I used the latter option by adding the conditional to it and that would be fine as a stop gap measure. But if@cart_itemdoes not exist, it should not even get to this part of the code. By addressing it in this way we would be treating the symptoms instead of treating the illness. And that is something as a developer that we want to avoid if at all possible. If you do not treat the illness itself, you are inviting new bugs to be created from the bug that you put a stop-gap measure on. Then because you have treated the symptoms, this could possibly hide the presence of these new bugs until it has created a tangled mess that is much more difficult to address than if you had fixed the original bug.

Be proactive, not reactive. To fix a bug is being proactive. Whatever action that was causing the bug to pop up, address it before its execution and prevent that action from happening. When you are reactive and just patching issues, you are allowing other bad things to happen as the code base grows.

That is all I have for today. Please leave your thoughts and/or comments below. Happy coding!

--

--