9 ms·
One of the major shifts in my coding style over the past ten years has been to increase the amount of duplication. My threshold for "I should really dedupe that
by Strilanc 3y ago
One of the major shifts in my coding style over the past ten years has been to increase the amount of duplication. My threshold for "I should really dedupe that" increased from ~3:7 lines to ~10:50. Looking back this was driven by two main factors: testing and performance optimization.
The testing side is just that tests become awful much faster than normal code if you dedupe them. Unit tests are supposed to be simple and independent, but deduping makes them correlated and complex. You think you'll make things simpler by extracting the common setup from twenty tests into one method, but instead you've coupled the tests so they can't individually be tweaked and laid the seeds for a monster incomprehensible test object to grow from.
The performance side is that often improving performance requires removing abstraction layers so everything is in one spot, allowing irrelevant cases to be removed. Adding the abstraction layers ahead of time makes performance worse to start with, from all the jumping and "paper over one more difference" flag checking, and also makes performance improvements harder later.
If two things are supposed to behave analogously, I'm nowadays much more likely to enforce this by testing the analogy rather than by sharing the implementation.