3 ms·
For folks who seek a rule of thumb, I’ve found SPoT (single point of truth) a better maxim than DRY: there should be ideally one place where business logic is d
by mplanchard 1y ago
For folks who seek a rule of thumb, I’ve found SPoT (single point of truth) a better maxim than DRY: there should be ideally one place where business logic is defined. Other stuff can be duplicated as needed and it isn’t inherently a bad thing.
To modulate DRY, I try to emphasize the “rule of three”: up to three duplicates of some copy/paste code is fine, and after that we should think about abstracting.
Of course no rule of thumb applies in all cases, and the sense for that is hard to teach.
- GMoromisato 1y ago100% agree. Duplication is far cheaper than the wrong abstraction. Student: I notice that you duplicated code here rather than creating an abstraction for both. Master: That is correct. Student: But what if you need to change the code in the future? Master: Then I will change it in the future. At that point the student became enlightened.
- deleted 1y ago[deleted]
- bluefirebrand 1y ago> To modulate DRY, I try to emphasize the “rule of three”: up to three duplicates of some copy/paste code is fine, and after that we should think about abstracting Just for fun, this more or less already exists as another acronym: WET. Write Everything Twice It basically just means exactly what you said. Don't bother DRYing your code until you find yourself writing it for the third time.
- noisy_boy 1y agoAnd to those who feel the OCD and fear of forgetting coming over by writing twice, put TODOs on both spots; so that when the third time comes, you can find the other two easily. If you are the backlogging type, put JIRA reference with the TODOs to make finding even easier.
- ghosty141 1y ago> I’ve found SPoT (single point of truth) a better maxim than DRY I totally agree. For example having 5 variables that are all the same value but mean very different things is good. Combining them to one variable would be "DRY" but would defeat separations of concern. With variables its obvious but the same applies to more complex concepts like functions, classes, programs to a degree. It's fine to share code across abstractions but you gotta make sure that it doesn't end up tying these things too much together just for the cause of DRY.