An Interest In:
Web News this Week
- April 19, 2024
- April 18, 2024
- April 17, 2024
- April 16, 2024
- April 15, 2024
- April 14, 2024
- April 13, 2024
What is DRY Code
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.
Original Link: https://dev.to/wizdomtek/what-is-dry-code-3imh
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To