A few years ago, I was working on a front-end project for a client.
I was running into a performance problem. An event was being triggered too often, and my code ran an expensive calculation every time.
So I spent a few hours writing a utility method that prevents a function from being called more than once in specified amount of time.
I was happy with my solution. Until the next day, when I found out that I had built my own inferior implementation of Underscore's
throttle instead of a custom function has lots of advantages:
_.throttleis well-tested and known not to be buggy
- Other developers already know how to use
- (Less code to download. I wouldn't worry about this too much.)
_.throttle is the better solution.
But I didn't change my code to use it.
I liked my code. I had put in a lot of effort to solve the problem in a re-usable way. Plus it signaled to the client that I had actually done some work.
It's natural to become attached to something you've made.
Throwing out code means admitting that you've spent your time making something useless. It's a classic example of a sunk cost skewing your decisions.
But more code means more complexity which makes it harder to maintain the codebase. When you become aware of a simpler solution, think about whether you can justify keeping the code that you've already written.
Emotional attachment to code leads to bad decision making. Recognize when it influences your decisions and re-examine the choice you've made.