Don't Repeat Yourself (DRY)
Avoid duplicating the same information in multiple places. The DRY principle encourages developers to eliminate code duplication by abstracting functionality.
Usage
π Guideline
Don't Repeat Yourself (DRY): Avoid duplicating the same information in multiple places.
The DRY principle encourages developers to eliminate code duplication by extracting common functionality into reusable components. This promotes code reusability, easier maintenance, and improved readability.
π οΈ How to Apply
- Extract Reusable Functions: Identify code blocks with similar functionality and extract them into reusable functions. Reuse these functions across your codebase. βοΈ
- Create Reusable Classes: Modularize your code by creating reusable classes that encapsulate common functionality. This allows you to reuse the same code across different parts of your application. π§©
- Refactor Duplicate Code: Regularly review your codebase and refactor any duplicate code you come across. Consolidate repeated logic into a single location to ensure consistency and reduce maintenance efforts. π
Pros and Cons
π Pros
- Code Reusability: DRY principles promote code reuse, making it easier to maintain and update your codebase. β»οΈ
- Improved Maintainability: By eliminating redundant code, you reduce the chances of introducing bugs and make it easier to locate and fix issues when they arise. π οΈ
- Enhanced Consistency: DRY code encourages consistent implementation, ensuring that changes made in one place propagate correctly throughout the codebase. π
π Cons
- Complexity Tradeoff: Extracting reusable components or functions may introduce additional complexity, requiring careful design and consideration to ensure clarity and understandability. π€
- Over-Abstracting: In some cases, excessive attempts to eliminate duplication can lead to over-abstraction, making the code harder to understand and maintain. π§©
- Increased Development Time: Applying DRY principles may initially require more effort upfront to identify and extract common code. However, this investment pays off in the long run by reducing duplication-related issues. β±οΈ
Examples
β Bad
function calculateRectangleArea(width, height) {
return width * height;
}
function calculateSquareArea(sideLength) {
return sideLength * sideLength;
}
function calculateTriangleArea(base, height) {
return (base * height) / 2;
}
β Good
function calculateRectangleArea(width, height) {
return width * height;
}
function calculateSquareArea(sideLength) {
return calculateRectangleArea(sideLength, sideLength);
}
function calculateTriangleArea(base, height) {
return calculateRectangleArea(base, height) / 2;
}
References
π Related principles
- Single Responsibility Principle (SRP): DRY aligns with the SRP by promoting the separation of concerns. Both principles aim to reduce code complexity and improve maintainability. π―
- Keep It Simple, Stupid (KISS): DRY and KISS both advocate for simplicity and avoiding unnecessary complexity. DRY focuses specifically on eliminating code duplication, while KISS emphasizes simplicity in overall design and implementation. π€
- Separation of Concerns (SoC): DRY aligns with SoC by reducing duplication and promoting modularity, making it easier to identify and manage different concerns independently. π§©