DRY
The idea that each piece of logic should exist in one and only one place in a system. Adhering to the DRY principle, we can ensure maintainability and scalability.
Advantages
- Readability: When each piece of logic exists in one place, it’s easier for other developers (and yourself) to understand the flow.
- Easier Maintenance: Any necessary changes or fixes need to be applied to the single location rather than multiple which in-turn reduces the chances of creating bugs or missing another part of the code to be fixed
- Easier Testing: When code isn’t duplicated, testing is straightforward since you only have to test one module or piece of code
- Development Efficiency
- Optimized Resource Usage: Less repeated code will most likely lead to faster execution and can have less memory consumption
- Less Error-Prone: Centralizing code has the benefit of less area for bugs. Repeating logic multiple places, changes to one might not be replicated in others, producing inconsistencies and likely errors
Some Simple Examples to get the Idea Across
UI Components:
Non-DRY: For every page in a website where you need a specific button style, you write the CSS for that button.
const Button1 = styled.button`
background-color: blue;
color: white;
// ... other styles
`;
const Button2 = styled.button`
background-color: blue;
color: white;
// ... other styles
`;
DRY: You define the button style once in a central CSS file and reuse that class on every page where the button appears.
const BaseButton = styled.button`
background-color: blue;
color: white;
// ... other styles
`;
const Button1 = styled(BaseButton)``;
const Button2 = styled(BaseButton)``;
Database Connection:
Non-DRY: Every time you need to connect to a database, you write the connection logic.
function addUser() {
const connection = /* database connection logic */;
// logic to add user
}
function deleteUser() {
const connection = /* database connection logic */;
// logic to delete user
}
DRY: You create a single function or class that handles database connections. Whenever you need to connect, you call this function or instantiate this class.
function getDBConnection() {
return /* database connection logic */;
}
function addUser() {
const connection = getDBConnection();
// logic to add user
}
function deleteUser() {
const connection = getDBConnection();
// logic to delete user
}
Data Validation:
Non-DRY: Before inserting or updating data in various parts of an application, you write validation logic for that data each time.
function saveUserData(user: any) {
if (!user.name || !user.email) {
throw new Error('Invalid user data');
}
// save user logic
}
function updateUserDetails(details: any) {
if (!details.name || !details.email) {
throw new Error('Invalid user data');
}
// update user logic
}
DRY: You create a single validation module or function for that data type and call it whenever validation is needed.
function validateUserData(data: any) {
if (!data.name || !data.email) {
throw new Error('Invalid user data');
}
}
function saveUserData(user: any) {
validateUserData(user);
// save user logic
}
function updateUserDetails(details: any) {
validateUserData(details);
// update user logic
}
Adhering to DRY I believe is essential for all developers. Looking back on the first code I ever wrote, I struggled with this at times. I also recall getting stuck on bugs and would always lose track of what things did and why they did it. By repeating certain parts of code I would easily get overwhelmed and frustrated. After learning about DRY and putting it into practice, I have felt more confident in the code because it is easier to read, test, and understand the system as a whole. I hope you find this information useful! Thanks for reading and always strive for the best!