3 ms·
The key factor when de-duping some code is to know whether the code is the same because they express the same abstraction or due to coincidence. If they are th
by jmull 3y ago
The key factor when de-duping some code is to know whether the code is the same because they express the same abstraction or due to coincidence.
If they are the same abstraction then they should always be the same and you're doing the right thing to de-dupe.
If they are the same due to coincidence de-duping will tie together things that should be independent. As development continues the implementations will need to diverge. That's when you get the rat's nest of conditional logic. It's a lot easier to add a parameter and conditional logic to a function than rip it out.
It's not always easy to tell if two bits of code are the same due to coincidence or not... it might come down to nuances of business considerations that the developer has no idea about (or, since we're talking about predicting the future, no one knows about).
I don't think it can be done perfectly. But it's worth considering why not to de-dupe before you do it.
- aleksiy123 3y agoI think this is the biggest thing people generally misunderstand about "duplication". It's really about if two concepts need to change together over time. They should be singularly defined. If they can move independently they should be two definitions. It's not literally about the code looking the same.
- kevincox 3y agoI was looking for this. There are definitely two types of duplication. For example not every use of the number 16 should be replaced with a SIXTEEN constant. However if the maximum allowed password length is 16 you shouldn't be writing 16 all over you code, you should be writing MAX_PASSWORD_CODEPOINTS because your system may depend on that value being consistent. Although I would disagree that you should never deduplicate things that are coincidentally the same. Sometimes code that is coincidentally the same can have the same bugs and require the same updates over time, so deduplicating them can reduce maintenance cost and remove bugs. However I wouldn't race to deduplicate these things. Just if they become frequent patterns or have remained the same for long enough to justify the effort to unify them.