What is DRY Code
"Don't repeat yourself" (DRY) is a principle of software development aimed at reducing repetition of software patterns,[1] replacing it with abstractions or using data normalization to avoid redundancy.
The DRY principle is stated as "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system".It usually means refactoring code by taking something done several times and turning it into a loop or a function. DRY code is easy to change, because you only have to make any change in one place.
Examples of Non-DRY and Dry Code
let drinks = ['lemonade', 'soda', 'tea', 'water'];
let food = ['beans', 'chicken', 'rice'];
//non DRY code
console.log(drinks[0]);
console.log(drinks[1]);
console.log(drinks[2]);
console.log(drinks[3]);
console.log(food[0]);
console.log(food[1]);
console.log(food[2]);
// DRY code
function logItems (array) {
for (let i = 0; i < array.length; i++) {
console.log(array[i]);
}
}
logItems(drinks);
logItems(food);
CONCLUSION
Whenever you finish writing some code, you should always look back to see if there is any way you can DRY it up, including:using descriptive variable names, taking repetitive bits of code and extracting them into a function or loop.
Connect me on facebook