4 ms·
Agreed that fanatical adherence to DRY is a mistake. I did this when I was first starting out: any code that looked similar at all would be factored out into a
by kibwen 9y ago
Agreed that fanatical adherence to DRY is a mistake. I did this when I was first starting out: any code that looked similar at all would be factored out into a new hyper-specific function, regardless of how unrelated the original pieces of code were. But this introduces extreme coupling between components that have no right being coupled, and makes evolving the code a nightmare (suddenly all your DRY-borne functions grow immense lists of configuration parameters to account for unforeseeable differences).
At the same time, let's not take this dogma too far in the other direction. Repeating oneself is still undesirable, but DRY-related refactoring should focus on behavior and intent, not on how the code looks. And abstractions are still good (they're how we get anything done at all!), but leaky abstractions should be avoided, indirection-via-abstraction should be minimized, and we should be vigilant against extreme and overeager overapplication of abstraction (insert your blub joke here).
- antonvs 9y agoBalancing these kinds of constraints - how much and when to DRY vs. copy - is a big part of what engineering is about. It also needs to take into account factors like available budget of time and money, capabilities of the team working on the code, etc.
- gowld 9y ago> But this introduces extreme coupling between components that have no right being coupled, and makes evolving the code a nightmare (suddenly all your DRY-borne functions grow immense lists of configuration parameters to account for unforeseeable differences). * A and B both depend on the same logic X. * You make a shared helper function X * Requirements for A change, necessitating a change to x. * You check if B requires the same change. If yes, change X. If no, fork X into X_A and A_B Now you're staying DRY, and DRY has saved the day by forcing you to think about whether A's change should impact B. If you had prematurely forked X, then X_A and X_B would drift without any sanity check on whether they should.