π‘ DRY & KISS Principles in JavaScript
When writing code, it's important to keep it clean, simple, and maintainable. Two core software design principles that help with this are DRY and KISS.
π DRY β Donβt Repeat Yourself
DRY means avoiding repetition in your code. Repeating the same logic or structure in multiple places increases the chances of bugs and makes code harder to maintain.
π₯ Bad Example:
let area1 = 10 * 5;
let area2 = 20 * 5;
let area3 = 30 * 5;
// Repeating the same formula
β DRY Version:
function calculateArea(length, width) {
return length * width;
}
let area1 = calculateArea(10, 5);
let area2 = calculateArea(20, 5);
let area3 = calculateArea(30, 5);
β
Tip: If you're copy-pasting code, it's a good sign you should refactor it.
π§ KISS β Keep It Simple, Stupid
KISS encourages you to keep your code simple and easy to understand. Avoid over-engineering and writing overly complex logic when a simpler alternative exists.
π₯ Bad Example:
function isEven(num) {
if (num % 2 === 0) {
return true;
} else {
return false;
}
}
β KISS Version:
const isEven = num => num % 2 === 0;
π§Ή Tip: Simple code is easier to debug, test, and collaborate on with teammates.
π§ͺ Final Thoughts
- DRY helps reduce bugs and future-proof your logic
- KISS makes your code easier to understand and maintain
- Together, they improve code readability and quality
Stick to these principles in every project β theyβre simple, but powerful habits that separate clean code from messy chaos.