DRY (Don't Repeat Yourself)
DRY (Don't Repeat Yourself) is one of fundamental software development principles that states: don't repeat yourself. Its main idea is to avoid code and knowledge duplication within one project. If we write same (or very similar) solutions several times, this increases error risk and complicates maintenance.
DRY principle essence can be described as: each piece of information in program should have single, consistent and authoritative representation. Simply put, if something in code needs to be repeated — better extract it into separate function, module or class and reuse.
// Bad
function calculateAreaOfRectangle(length, width) {
return length * width;
}
function calculatePerimeterOfRectangle(length, width) {
return 2 * (length + width);
}
// Better
function calculateRectangleProperties(length, width) {
const area = length * width;
const perimeter = 2 * (length + width);
return { area, perimeter };
}