SRP, DRY, KISS, YAGNI – Clean Code Principles You Must Know

By technoayan
0views
SRP, DRY, KISS, YAGNI – Clean Code Principles You Must Know

Learn the most essential clean coding principles - SRP, DRY, KISS, and YAGNI. Understand them with real-world examples, Java code, and how they improve software design.

🧼 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:

JAVA
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:

JAVA
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:

JAVA
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:

JAVA
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:

JAVA
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:

JAVA
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:

JAVA
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:

JAVA
public class Payment {
    public void payByCreditCard() {
        // logic
    }
}

Add crypto or UPI only when needed.


📌 Summary Table

Principle
Meaning
Purpose
SRPOne class, one responsibilitySeparation of concerns
DRYDon’t repeat codeReuse logic
KISSKeep it simpleAvoid complexity
YAGNIDon’t write unused featuresSave 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:

JAVA
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));
    }
}

Thanks for reading!

technoayan

Author & Tech Enthusiast

"Keep learning, keep growing, and keep sharing knowledge with the world."

Show Some Love

Let the author know you enjoyed this content

0
people like this

Sign in to show your appreciation

Share This Post

Help others discover this content

Rate This Post

Share your thoughts and help others discover great content

Sign in to rate this post and share your feedback

Community Rating

No ratings yet. Be the first to rate this post!

More in LLD

Comments (0)

Leave a Comment

No comments yet. Be the first to share your thoughts!