Before moving into the DRY principle, let's understand the goals of a good programmer:
- Solving Problems
- Writing Amazing Code
- Maintainsability
- Simplicity
- Cleanliness
- Optimization
Why write clean, simple, and maintainable code?
You're writing for humans.
- Whether it's for a team.
- When you revisit your code 6 months later, it must be understandable by you.
Is there a standard way to write clean code?
Yes! Here are some principles:
- DRY
- KISS
- YAGNI
- SOLID
We often want our code to handle every imaginable situation or future feature.
However, this mindset leads to bloated code, wasted time, and unnecessary complexity.
This is where YAGNI comes to the rescue.
Let's Deep Dive...
In this article, we will cover:
- What is YAGNI?
- Common Pitfalls of Ignoring it
- Why is it Important?
- How to Apply it?
- When Not to Use it?
- Disadvantages of YAGNI
What is YAGNI?
Only build something when you really need it - not just because you think you might need it later.
The YAGNI principle is a core concept in software development, especially within agile methodologies.
It simply means:
- Focus on what’s needed right now.
- Don’t waste time adding features you might need "someday" but aren't required today.
Example Codes: Non-DRY vs DRY
Non-YAGNI (Violating YAGNI)
class PaymentProcessor {
private:
std::string paymentMethod;
public:
PaymentProcessor(std::string method) : paymentMethod(method) {}
void processPayment(double amount) {
if (paymentMethod == "CreditCard") {
std::cout << "Processing payment of $" << amount << " via Credit Card." << std::endl;
} else if (paymentMethod == "DebitCard") {
std::cout << "Processing payment of $" << amount << " via Debit Card." << std::endl;
} else if (paymentMethod == "PayPal") {
// Unnecessary feature
std::cout << "Processing payment of $" << amount << " via PayPal." << std::endl;
} else if (paymentMethod == "Crypto") {
// Unnecessary feature
std::cout << "Processing payment of $" << amount << " via Cryptocurrency." << std::endl;
} else {
std::cout << "Payment method not supported." << std::endl;
}
}
};
int main() {
PaymentProcessor processor("CreditCard");
processor.processPayment(100);
PaymentProcessor invalidProcessor("PayPal");
invalidProcessor.processPayment(58);
}
- Adds unnecessary features (PayPal and Crypto) not requested.
- Wastes development time on irrelevant features.
- Increases complexity of the codebase.
- Risk of disqualification in interviews by violating YAGN
🛑 Always implement functionality exactly as requested in interviews to avoid breaking YAGNI and risking disqualification!
YAGNI (Adhering to YAGNI)
class PaymentProcessor {
private:
std::string paymentMethod;
public:
PaymentProcessor(std::string method) : paymentMethod(method) {}
void processPayment(double amount) {
if (paymentMethod == "CreditCard") {
std::cout << "Processing payment of $" << amount << " via Credit Card." << std::endl;
} else if (paymentMethod == "DebitCard") {
std::cout << "Processing payment of $" << amount << " via Debit Card." << std::endl;
} else {
std::cout << "Payment method not supported." << std::endl;
}
}
};
int main() {
PaymentProcessor processor("CreditCard");
processor.processPayment(100);
}
- Meets the interviewer's requirements.
- Keeps the code simple and focused.
- Demonstrates good understanding of the YAGNI principle.
Common Pitfalls of Ignoring it
- Excessive complexity: Code bloats with unused features.
- Wasted time and resources: Building unnecessary functionality.
- Harder maintenance: More bugs and difficulty understanding the codebase.
Why is it Important?
YAGNI prevents overengineering, keeping developers focused on what's truly needed.
- Simplicity: A leaner and clearer codebase.
- Faster development: Time isn't wasted building unneeded features.
- Easier maintenance: Fewer features mean fewer places for bugs to hide.
YAGNI also:
- Reduces project timelines.
- Lowers technical debt.
- Boosts team productivity.
How to Apply it?
- Gather necessary requirements: Understand what is truly needed.
- Communicate with your team: Stay aligned on project goals.
- Plan simply: Solve only today's problem.
- Reject unnecessary features: If not needed now, don't build it.
- Track progress: Keep the solution lean throughout development.
Related Concepts
- KISS Principle (Keep It Simple, Stupid): Encourages simplicity in design and code.
- DRY Principle (Don't Repeat Yourself): Encourages reducing duplication in code and logic.
Both articles are available on my profile - feel free to check them out for detailed explanations!
Disadvantages of KISS
- Limited Flexibility: Simplified designs may struggle to accommodate future changes or scalability.
- Potential Oversimplification: Striving for simplicity can cause missing critical edge cases or important features.
- Misinterpretation of Simplicity: Equating simplicity with fewer lines can compromise readability.
- Resistance to Innovation: Excessive focus on simplicity may discourage using better frameworks or performance improvements.
The KISS Principle is a powerful guide to maintain simplicity in codebases.
By embracing the KISS principle, we ensure that our code remains readable, maintainable, and error-free.
Although it's essential to strike a balance between simplicity and functionality, adopting a simple approach generally leads to faster development cycles and more efficient problem-solving.
💬 Did you find this useful?
If this article helped you, feel free to like and share!
Have any questions or ideas?
Leave a comment below — I'd love to hear from you! 🙌