Premature abstraction

TL;DR - Stick to something like the rule of three. Premature abstraction in any system is almost always going to be problematic, expensive and frustrating. Never add abstractions because you “might” need it in the future. Add it when you “really” need it

Abstraction is a good way of hiding complexity. In your software development life, you probably have created well rounded abstractions that made your life easier. While abstractions make things cleaner and easier to work with, premature abstraction will have the opposite effect.

Imagine working on a new feature. You start building parts of it either top down or bottom up (I tend to prefer top down over bottom up). Almost immediately you start seeing little patterns of duplicated code. Voila! You make an abstraction and it is beautiful. Life continues, you get your tasty lunch, get that coffee and go back to writing more code.

You start adding more features. To work with the growing complexity you try to make your abstraction more accommodating. Before you know it the abstraction that started to look like a TV remote has now become a microwave oven that can also tell you a joke, and has sprouted horns. The problem is not that creating that abstraction is bad. It is just the timing of it all. The abstraction got created too soon. All the duplicated code that seemed messy and hard to maintain is just in a different place. It is probably even harder to maintain this code now.

The maintainable way of creating abstractions is to fully understand your problem domain first. If you feel you will need this abstraction right at the start, stop and think about it for a little bit. There could very well be just one instance of this and you may not need it at all. The next instance might be entirely different. If you wait until you see more need for the abstraction, it is not anymore complex or expensive to introduce it at a later time. On the other hand, if you introduce it now and want to get out of it after you’ve made it incredibly complex, it will be expensive and frustrating.

The rule of three as popularised by Martin Fowler is probably a good rule to follow ¯\_(ツ)_/¯. I personally try to follow it.