CHAPTER 08
Intermediate
DRY, KISS, and YAGNI Principles
Updated: May 16, 2026
25 min read
# CHAPTER 8
DRY, KISS, and YAGNI Principles
1. Introduction
Before diving into complex object-oriented design patterns, every software engineer must master three foundational acronyms. These principles act as the immune system of a codebase, defending it against the dual diseases of copy-paste programming and massive overengineering. They are DRY (Don't Repeat Yourself), KISS (Keep It Simple, Stupid), and YAGNI (You Aren't Gonna Need It). In this chapter, we will explore these philosophical pillars. We will learn how to extract duplicate logic, strip away unnecessary architectural complexity, and resist the temptation to build features for a future that may never arrive.2. Learning Objectives
By the end of this chapter, you will be able to:- Define and apply the DRY principle to eliminate duplicate code.
- Understand the danger of copy-paste programming and maintenance nightmares.
- Apply the KISS principle to favor simple, readable solutions over complex architectures.
- Utilize the YAGNI principle to prevent premature optimization and overengineering.
- Balance reusability with simplicity.
3. DRY (Don’t Repeat Yourself)
*Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.*-
The Problem: A developer needs to calculate tax. They write the formula in the
CartController. Later, they need it in theInvoiceService. They copy and paste the formula. Next year, the government changes the tax rate. The developer updates the Cart, but forgets the Invoice. The system is now mathematically corrupt.
-
The Solution: Extract the formula into a single
TaxCalculatorclass. Both the Cart and the Invoice call this one class. If the law changes, you update the code in exactly *one* place.
4. KISS (Keep It Simple, Stupid)
*Most systems work best if they are kept simple rather than made complicated.*- The Problem: Developers love building complex things. To solve a simple problem (like parsing a CSV file), a developer might build a massive, multi-layered, abstract factory framework with 12 interfaces.
-
The Solution: Does a simple
whileloop work? Use thewhileloop. Do not introduce Design Patterns, complex inheritance, or new frameworks unless the problem is so complex that a simple solution is no longer viable. Complexity is a liability, not an asset.
5. YAGNI (You Aren’t Gonna Need It)
*Always implement things when you actually need them, never when you just foresee that you need them.*- The Problem (Overengineering): The Product Owner asks for a simple blog page. The developer spends 3 weeks building a highly scalable, multi-tenant caching layer because "we might have 10 million users next year."
- The Reality: 90% of the features you build "just in case" are never used. They become dead code that the team has to maintain forever. It wastes money and slows down development.
- The Solution: Build exactly what is required to fulfill today's User Story. If you hit 10 million users next year, refactor the code *next year*.
6. The Balancing Act
These principles can conflict if applied blindly.- Trying to make code perfectly DRY can sometimes result in abstractions so complex that it violates KISS.
- *Rule of Thumb (The Rule of Three):* Do not apply DRY the very first time you copy code. Wait until you have written the exact same code *three* times. Then, the pattern is obvious, and it is time to extract it into a shared function. This prevents premature, incorrect abstractions.
7. Diagrams/Visual Suggestions
*The Cost of YAGNI Violations*
txt
8. Best Practices
- Delete Dead Code: If you built something violating YAGNI 2 years ago, and it is still sitting unused in the codebase, delete it immediately. It is polluting your search results and confusing new developers. Version Control has a backup if you ever need it.
9. Common Mistakes
-
The Wrong Abstraction (Forced DRY): Two functions happen to have 5 lines of identical code *by coincidence*, but they represent entirely different business rules (e.g., calculating a user's age vs calculating a server's uptime). A developer forces them to share a function to be "DRY." When the business rules inevitably diverge, the shared function becomes a tangled mess of
if/elsestatements. Duplication is cheaper than the wrong abstraction.
10. Mini Project: Refactor for DRY
Scenario: Remove the duplication. *Before (Violating DRY):*
php
*After (Applying DRY):*
php
11. Practice Exercises
- 1. Define the YAGNI principle. Why is building features "just in case we need them later" a massive financial waste for a software company?
- 2. Explain the "Rule of Three" in relation to the DRY principle.
12. MCQs with Answers
Question 1
What is the primary danger of copy-pasting code (Violating the DRY principle)?
Question 2
A developer uses a highly complex Machine Learning algorithm to sort an array of 5 items, arguing it is more "advanced" than a standard loop. Which principle is being blatantly violated?
13. Interview Questions
- Q: Explain a situation where "Duplication is cheaper than the wrong abstraction." When might forcing the DRY principle actually harm a codebase?
- Q: A junior developer spends a week building an elaborate caching mechanism for a database table that currently has only 50 rows, citing future scalability. As an architectural reviewer, how do you explain YAGNI to them?
- Q: How do the principles of KISS and YAGNI align directly with the Agile manifesto's focus on iterative, incremental delivery?
14. FAQs
Q: Does DRY apply to database schemas? A: Yes. This is called "Database Normalization." If a customer's address is stored in theUsers table and the Orders table, and the customer moves, you have to update two places (a nightmare). Store it in one Addresses table and link to it.