"Don’t repeat yourself", or "DRY", is a programming adage intended to capture the essence of the truth that duplications lead to inconsistencies, which lead to bugs. In my opinion it is one of the more profound truths of programming, not only because it is a good practice for the working developer, but also because it’s contemplation leads to better design (though sometimes at the cost of simplicity).
Simplicity might be sacrificed
In trying to achieve DRYness, you might overoptimize at the cost of another powerful truth often captured in the phrase KISS, which is that simplicity is paramount.
A great example of the tension between simplicity and singularity (i.e., non-repitition) is using strings in your codebase. The situation is as such: There is a few strings in a codebase that are used to control flow, typos will cause bugs, and they are used repeatedly throughout the codebase. DRY would suggest a dictionary reference to give you singularity:
const dictionary = {
important: ‘important’,
words: ‘words’,
here: ‘here’
}
function idk(word) {
if (word === dictionary.important) {
return true
}
}
This is a great example, in my opinion, because it is either a good idea, or harmful overkill, depending on the context. It’s a good idea because you’ve rid yourself of typos that would be hard to debug and are error prone. It’s harmul overkill because you lose the simplicity of a one-line string check, and imagine trying to combine these all together in an error-less way,
const x = capitalize(dictionary.important) + dictionary.words + ‘go’ + dictionary.here + ‘.’
const y = ‘Important words go here.’
The lack of simplicity in x
is more likely to cause bugs that the duplication of strings of y
.
Wet code is "not DRY"
You might hear a developer talk about "wet" or "soggy" code, which is an allusion to DRY. It means there is a lot of duplication in the codebase, a lot of repetition, and therefore is likely to be buggy. Wet code is as bad as DRY code is good.