Snippet Podcast: Should I delete this?

Snippet is a weekly podcast where I share snippets of coding knowledge.

Originally posted on https://tiagojdf.com/

Last week I talked about “Less is more” and how you should not add code because you might need it in the future. Today, in our efforts to simplify code and to do more with less, I want to talk about deleting existing code.

First of all, deleting code is hard. Deleting code to simplify it or to completely remove it, can cause all kinds of issues, so before you start deleting code you should ask yourself should I remove this code? In order answer this, I really like to think about a quote from Chesterton. If you are curious, you can listen to the quote in the end of this episode, but for now, let’s consider a simplified version of it, adapted to the problem of deleting code. Here it goes:

“Don’t ever delete code until you know the reason why it was wrote

If you remember, last week I delete some frontend code for showing images, but before doing so I asked myself: Can I do this?

I asked myself: Why is this code here, if it is never used? In order to answer this, I went and looked into the backend code into the templates and into the existing code and only by looking at it I realized that the code in question was added for a future case that never happened.

Another way to understand the purpose of the code before deciding if you can delete it, is looking for documentation or tests. Tests are great documentation by the way. Not only do they show you how the code works in a controlled environment, but also they are often added to test edge cases or to fix bugs.

Sometimes even then, the answer is still not clear. In those cases I do a git blame and see why was this code added. If the blame shows me when the code was changed, but doesn’t answer the why it was added, I go to the parent commit and redo a git blame. And I keep doing this until I figure out why was this code added in the first place, and if that is still a valid reason for it to exist.

Sometimes there is a valid reason for it to exist. For example, that code deals with an edge case; or it’s there to fix a browser inconsistency; or it’s not called directly in the code, but some abstract piece of code ends up calling it; etc. etc. etc.

But sometimes, the code is no longer valid. For example, someone forgot to delete it when refactoring; or you stopped using because your product no longer has this functionality; or maybe it’s just a case of “We might need this in the future”.

When that is the case, then go ahead, and delete that code, and remember, Less is more.

This is Chesterton’s original quote that inspired this episode:

There exists in such a case a certain institution or law; let us say, for the sake of simplicity, a fence or gate erected across a road. The more modern type of reformer goes gaily up to it and says, “I don’t see the use of this; let us clear it away.” To which the more intelligent type of reformer will do well to answer: “If you don’t see the use of it, I certainly won’t let you clear it away. Go away and think. Then, when you can come back and tell me that you do see the use of it, I may allow you to destroy it.”

You can subscribe to the podcast here.

Dev at Sketchfab