DRY vs RYE
• John Vandivier
DRY is a good first-pass rule of thumb, but RYE is optimal. “Repeat yourself efficiently”
Examples:
- Add a data-attr to an HTML element so that some data can be read from it using $elem.data() rather than a longer jQuery selector chain $elem.closest(‘.thing’).find(‘otherthing’).val()
- Benefits of concision, readability, and performance
- Database normalization
- Foreign key relationships are duplicate but useful to improve database query speed when only a subset of information is needed
- Complexity management
- When a URL or string is repeated many times, put it in a constants service for scale and maintenance
- But, when a constant is only repeated once, twice, or even a few times this doesn’t make sense. You are adding a service to develop and integrate with very little benefit. In particular, consider strings that are unlikely to ever need an update, such as $el.css(‘color’, color); that color string will prob never change. Also consider numbers – why have CONSTANTS.FIVE_HUNDRED instead of 500? Even if 500 is used 500 times, it wouldn’t make sense to make this a constant. Constants are useful in part so you have a central location to update; when will you ever need to update the constant value FIVE_HUNDRED to anything other than 500? You won’t. That’s overengineering.
I would say, though, “When in doubt, DRY it out”