Code is Cheap, Knowledge is Expensive
June 9th, 2021
Imagine this scenario. You need to add a feature to your code. You spend some time trying to understand the code, then some more time refactoring it so that your feature would be easier to add, then you implement the feature. When you push your code, the feature is fine, but your teammates aren't entirely happy with the direction of the refactoring. It's better to have done it in two steps, the refactoring and then the feature, so that you could focus discussions and even postpone some refactoring that isn't critical for the feature.
However, the work is already done, and you can't really split the two tasks without basically redoing all the work from scratch. It's a shame to delete all this perfectly working code, yet the feature won't go through will all the other changes tagging along. It is now held hostage.
Even in retrospect, you could not have done the two tasks separately. You needed to work on the feature to better understand how to refactor the code. What would I have done in this situation? I would have saved the changes as a patch file for reference, then deleted my branch and started over, without any regrets. That is because code is cheap.
What took longest was not the code. When I develop, I spend the vast majority of my time reading the code and the docs, thinking, taking notes, thinking some more, discussing with colleagues, researching online, then eventually writing a bit of code. The most expensive activity is acquiring knowledge. When I delete code, I lose a negligible percentage of my work. The knowledge remains, either in my head, my notes or the patch file that I saved.
I can not only replicate the refactoring in record time, but with the full understanding at the end of the previous exercise, I will probably refactor things even better. I will push the code, review it with a colleague and then proceed with implementing the feature, again, possibly much better than the first time when I still possessed only partial knowledge.
So go ahead. Delete code. It's not as precious as you think.