DRY, KISS & YAGNI    Principles

 

This principle says about to make your code simple.

You should avoid unnecessary complexity.

Write a simple code it’s easier to maintain and easier to understand.

KISS is an acronym for Keep It Simple, Stupid.

Applying this principle in the design and in the implementation of the code:

  • Make your code simple.
  • Avoid unnecessary complexity.
  • Eliminate duplicated code.
  • Remove unnecessary features.
  • Don’t use unnecessary variables and methods.
  • Use names for variables and methods that makes sense and matches their responsibilities.
  • Separate the responsibilities of your classes.
  • Don’t need to implement something new to attend your needs.

Benefits of KISS principle:

  • Ease of Understanding: Simple solutions are easier to understand for both developers and end-users.
  • Performance: Simple code often performs better.

  • Reduced Errors: Simplifying solutions can help minimize the risk of errors or bugs.

  • Improved Maintenance: Simple systems are easier to maintain over time.

  • Faster Development: Keeping things simple often leads to faster development cycles.
  • Enhanced Scalability: Simple designs are more adaptable to changes and can scale more effectively.
  • Reduced Technical Debt: Complex codebases tend to accrue technical debt over time.
  • Better User Experience: Simple interfaces and designs tend to offer a better user experience.

DRY stands for Don’t Repeat Yourself.

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

This principle helping to avoid repetition of the same information (same code produced multiple times).

Applications of the DRY principle which help you create cleaner programming:

  • Divide code into smaller pieces of functionality, which can be reused easily.
  • Extract commonly repeated code into methods to centralize its logic and avoid duplicating it across our application.
  • Use classes and inheritance, and interfaces.
  • Create reusable components, functions, or modules that can be utilized in various parts of the codebase.

Duplicated code inspection

Advantages of applying the DRY principle in coding:

  • Maintainability: DRY code is easier to maintain because changes or updates only need to be made in one place, reducing the risk of inconsistencies.
  • Readability: DRY promotes cleaner and more readable code by eliminating unnecessary repetition, making it easier for developers to understand the logic.
  • Efficiency: Reusing existing code instead of duplicating it saves development time and effort, resulting in more efficient and productive workflows.
  • Consistency: DRY ensures consistent behavior throughout the codebase, as a specific piece of logic exists in only one place.
  • Reduced Bugs: DRY reduces the chances of introducing bugs or errors caused by inconsistent changes in duplicated code.

YAGNI stands for You Aren’t Gonna Need It.

This principle is based on the idea that adding unnecessary features can lead to increased complexity, longer development times, and potentially more bugs. 

Applying this principle to avoid unnecessary functionality.

  • Don't add unnecessary functionality that can lead to increased complexity and risk of introducing bugs.
  • Focus on providing the simplest solution that meets current requirements.
  • Focus on delivering the most important features first, which can ensure that the software meets the user’s needs and expectations.
  • Don't try to anticipate and accommodate potential future needs.

Benefits of YAGNI:

  • Faster Development: By focusing only on what is needed at the moment, can lead to faster development cycles and more efficient use of resources. 
  • Simplicity: Unnecessary features can add complexity to the codebase, making it harder to maintain and understand.
  • Flexibility: By avoiding unnecessary features, developers can keep the codebase flexible and adaptable to change.
  • Reduced Risk: By avoiding unnecessary features, developers can reduce the risk of introducing bugs and other issues into the codebase.

  • User Focus: By only implementing features that are necessary for the user, developers can ensure that the software meets the user’s needs and expectations.
  • Cost Savings: By avoiding unnecessary features, developers can save time and resources that would otherwise be spent on implementing and maintaining those features. This can lead to cost savings for the organization.
  • Improved Maintainability: A simpler codebase is easier to understand and maintain, making it easier for developers to make changes and fix bugs.
  • Better User Experience: By focusing on delivering the most important features first, developers can ensure that users get the functionality they need sooner, leading to a better overall user experience.