3 ms·
Duplication is usually a safe default course of action because you are not locking yourself in to any particular consensus of the problem domain. Obviously, too
by bob1029 6y ago
Duplication is usually a safe default course of action because you are not locking yourself in to any particular consensus of the problem domain. Obviously, too much of this will render a codebase a nightmare to maintain as bugfixes and feature enhancements have to be applied in multiple places.
I have found that starting with duplication is by far the easiest and most flexible way to work through problem domains that are complex. Once you have a really good grasp of the modeling, then you can iterate and decide on normalization where appropriate.
Thinking about this from an analytical perspective - If you build your application with duplication by default (i.e. define a domain model for each logical use case/scenario), then you will have an excellent analysis already in front of you regarding which business types should be normalized and which ones might be a little bit trickier to make common. Many times it is impossible to fully explore a problem domain until you have already written software against its entire extent.
- xnx 6y agoAnd often the process of de-duping repeated code later on isn't as bad as DRY purists make it out to be, especially since it can be done incrementally. Example: If you have similar functions in 7 places, you can consolidate them one by one. If you have 1 function used in 7 places, you have to consider all implications for all of those code paths.
- azundo 6y agoThis seems like an equivalent duality to me but in the case of seven independent functions you're much more likely to miss considering a case. If behavior changes for one you likely should be considering if that change applies to all the others.
- Ma8ee 6y agoBut when you set out to do that you first have to make sure they really are identical, and since everything from indentation to names often differ it might not be so automatic. And then you do find differences, and you have to figure out if they are there by accident because someone forgot to implement a fix or a change in some places or if the differences are intentional. On the other hand, if you realise your abstraction was bad, duplicating a function is always trivial.
- chousuke 6y agoIn my experience, the "later" may come too late. I've seen code copy-pasted and slightly modified over a dozen times, sometimes without even eliminating dead code! Copying a function or even a whole file is fine if you actually take a moment to consider whether it needs refactoring or not, but more often than not people will just copy and bash at the code until things work without actually making a conscious choice to duplicate code over refactoring.
- jtdev 6y agoYes, yes, yes! I wish devs could relax just little about duplication. It’s far less harmful than bad abstractions built with the only purpose of checking the DRY box.