A little copy-paste is cheaper than the wrong abstraction
One time I deleted what seemed to be a duplicated file, but it reappeared. I deleted it again, and the same thing happened. I did this three times before realizing it was an important file that kept regenerating itself for some reason. ๐ The reflex we all learned wrong At some point in your initial year, you received the same advice that I did. If you see two similar blocks, separate them. Rightโฆ
In a recent discussion, an individual recounted an experience where they deleted what appeared to be a duplicated file multiple times before realizing it was an important file that kept regenerating itself. This anecdote highlights a common reflex that many people learn incorrectly - the belief that duplicating code is always wrong.
The author argues that this reflex stems from a knee-jerk reaction to the principle of DRY (Don't Repeat Yourself), which has become deeply ingrained in the software development culture. While reducing repetition is important, it is not the ultimate goal of software development, which is about handling complexity. Sandi Metz, in her 2014 RailsConf talk "All the Little Things," emphasized that duplication is often cheaper than the wrong abstraction.
In 2016, Metz elaborated further in her blog post "The Wrong Abstraction," explaining that a bad abstraction can turn into a condition-laden process that weaves together loosely related concepts. The author cautions that a poorly designed abstraction can lead to a situation where no one dares to refactor it, causing it to accumulate special cases and flags over time.
They argue that the fastest way to correct this is to revert to duplicating the code. The author points out that duplicated code, while not elegant, has a local blast radius - a change in one place only affects that single location. On the other hand, a poorly designed abstraction can have a global impact, requiring changes in multiple places.
They highlight the importance of patience, suggesting that developers should wait and see if duplicated code mutates simultaneously before deciding to merge them. If not, it's better to avoid creating a landmine by prematurely implementing an abstraction. The author concludes by emphasizing that while DRY is not incorrect, it was applied too early in the software development process.
The real mistake is making assumptions about the abstraction before the code clarifies its purpose. They encourage developers to wait for the "scream" - a clear indication that the abstraction needs to be refined - before eliminating duplication.
Written by urgent.news from Dev.to's reporting โ not their text. Machine-written โ may contain errors; check the original before relying on it.