🧼 SRP, DRY, KISS, YAGNI – Clean Code Principles You Must Know
These are not just fancy acronyms—they are must-know principles for writing clean, maintainable, and scalable code.
🧠 1. SRP – Single Responsibility Principle
Each class should have only one reason to change.
Part of SOLID principles.
🚫 Bad Example:
public class Report {
public void generateReport() {
// logic to generate report
}
public void saveToFile() {
// logic to save report to file
}
public void sendEmail() {
// logic to email report
}
}
Here, one class is doing 3 responsibilities — breaks SRP.
✅ Good Example:
public class ReportGenerator {
public void generateReport() {
// logic
}
}
public class FileSaver {
public void saveToFile() {
// logic
}
}
public class EmailSender {
public void sendEmail() {
// logic
}
}
Now each class has one reason to change — follows SRP.
♻️ 2. DRY – Don’t Repeat Yourself
Avoid duplicating logic. Move repeated code into common methods or utilities.
🚫 Bad Example:
public class Invoice {
public double calculateGST(double amount) {
return amount * 0.18;
}
}
public class Bill {
public double calculateGST(double amount) {
return amount * 0.18;
}
}
Same logic repeated in two places.
✅ Good Example:
public class TaxUtil {
public static double calculateGST(double amount) {
return amount * 0.18;
}
}
Now both Invoice
and Bill
can use TaxUtil.calculateGST()
.
🤏 3. KISS – Keep It Simple, Stupid
Write simple, readable code. Don’t over-engineer.
🚫 Bad Example:
public int factorial(int n) {
return IntStream.rangeClosed(1, n)
.reduce(1, (a, b) -> a * b);
}
Looks cool, but not simple for beginners.
✅ Good Example:
public int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
Readable, maintainable. Simplicity wins.
🙅 4. YAGNI – You Aren’t Gonna Need It
Don’t build features you don’t need yet.
🚫 Bad Example:
public class Payment {
public void payByCreditCard() {}
public void payByCrypto() {} // Not needed now
public void payByGold() {} // 😅
}
You're writing for the future that might never come.
✅ Good Example:
public class Payment {
public void payByCreditCard() {
// logic
}
}
Add crypto or UPI only when needed.
📌 Summary Table
| Principle | Meaning | Purpose | | --------- | ----------------------------- | ------------------------------ | | SRP | One class, one responsibility | Separation of concerns | | DRY | Don’t repeat code | Reuse logic | | KISS | Keep it simple | Avoid complexity | | YAGNI | Don’t write unused features | Save time & reduce maintenance |
💡 Why These Matter in LLD
These principles help you:
- Write code that’s easy to test, debug, extend
- Avoid spaghetti code
- Make your design modular and clean
- Reduce tech debt
🛠 Example Scenario: User Registration Service
✅ Good Design Applying All 4:
class User {
String email;
String password;
}
class UserRepository {
void save(User user) {
// Save to DB
}
}
class PasswordEncryptor {
String encrypt(String password) {
return "hashed_" + password;
}
}
class UserService {
private final UserRepository repo = new UserRepository();
private final PasswordEncryptor encryptor = new PasswordEncryptor();
void register(String email, String password) {
String encrypted = encryptor.encrypt(password);
repo.save(new User(email, encrypted));
}
}