Duplication is far cheaper than the wrong abstraction - Sandi Metz
I was thinking about decision making lately with a wish to synthesize something into principles so that I don’t have to decide again and again in similar situations. But should I reach out to principles when something happened for the first or second time? I was thinking if programming can shed some light on this.
Imagine a software engineer working on a feature, and getting stumbled across something like
# somewhere in code do_foo do_bar # elsewhere in code do_foo do_bar
And your engineering instinct immediately whispers in your ear that “Don’t repeat yourself”, the famous DRY principle that every professional software engineer knows and abuses it
# somewhere in code - do_foo - do_bar + i_am_dry # elsewhere in code - do_foo - do_bar + i_am_dry
def i_am_dry do_foo do_bar end
However, we all know that code are meant to be changed as requirements(customer needs) change all the time, but any elite software engineer earning 6 figures a year must have heard about Refactoring
# somewhere in code - do_foo - do_bar + i_am_dry # elsewhere in code - do_bar - do_baz + i_am_dry
def i_am_dry(A) if some_condition(A) do_bar do_baz else do_foo do_bar end end
Eventually we may end up with code that is super “DRY”, as Sandi Metz famously endowed it as The Wrong Abstraction. But we are confident about ourselves because it’s consistent. What’s even better is that our career gets elevated this way and we become the domain expert who’s essentially irreplaceable for the job, proudly 🚢 code that we ourself can not understand after six months.
# somewhere in code - do_foo - do_bar + i_am_super_dry # elsewhere in code - ... + i_am_super_dry ... # elsewhere in code - ... + i_am_super_dry
def i_am_super_dry(A, B, ..., Z) case true when some_condition(A) ... when some_condition(B) ... when some_condition(Z) ... else do_foo do_bar end end
I am not a fan of over-engineering or premature optimizations, so I generally embrace the Rule of three from Martin Fowler. And I firmly believe that the cost of wrong abstraction is way higher than no abstraction at all. Without fully understand the situation, we can’t even be sure if the situations are similar to each other or not. Therefore, it might be okay to just let the randomness rule from time to time.