DEV Community

Cover image for Clean Code Essentials: YAGNI, KISS, DRY
Ravin Rau
Ravin Rau

Posted on

Clean Code Essentials: YAGNI, KISS, DRY

In the world of software engineering, where clean code and efficient design patterns reign supreme, three principles stand out as guiding lights: YAGNI, KISS, and DRY. These principles are not just buzzwords; they are the foundation of effective software development. Whether you're a seasoned developer or just starting your coding journey, understanding these concepts can transform the way you approach building software.

YAGNI - KISS - DRY

In this article, we'll dive into the heart of these principles. We'll explore how YAGNI (You Aren't Gonna Need It) helps prevent unnecessary complexity, how KISS (Keep It Simple, Stupid) encourages straightforward solutions, and how DRY (Don't Repeat Yourself) promotes code reusability. By the end, you'll see how these principles can lead to cleaner, more maintainable code and ultimately make you a more efficient software engineer. So, let's get started!


YAGNI - You Aren't Gonna Need It.

It's a principle in software development that reminds us to only implement features when they are actually needed, not when we think they might be needed in the future. The idea is to avoid over-engineering and keep things simple.

Yagni Principle

Why Do We Need to Use YAGNI?

Using YAGNI helps prevent unnecessary complexity in your code. When you add features or code that you don't currently need, you increase the chances of introducing bugs and make your codebase harder to maintain. By sticking to YAGNI, you focus on what's essential, saving time and resources.

How to Use the YAGNI Principle

Yagni Principle In Action

To apply YAGNI, always ask yourself if a feature is necessary right now. If it's not, don't build it. Prioritize the current requirements and resist the temptation to plan for every possible future scenario. This approach keeps your code lean and focused.


KISS - Keep It Simple, Stupid.

It's a principle in software development that encourages simplicity in design and implementation. The idea is to avoid unnecessary complexity and make things as straightforward as possible.

Kiss Principle

Why Do We Need to Use KISS?

Using the KISS principle helps make your code easier to read, understand, and maintain. Complex solutions can be difficult to debug and extend, while simple solutions are more robust and flexible. By keeping things simple, you reduce the risk of errors and make it easier for others (and your future self) to work with your code.

How to Use the KISS Principle

Kiss Principle

To apply KISS, always aim for the simplest solution that meets the requirements. Avoid overengineering and resist the temptation to add unnecessary features or complexity. Focus on clarity and straightforwardness in your code and design.


DRY - Don't Repeat Yourself.

It's a principle in software development that emphasizes the importance of reducing repetition in your code. The idea is to have a single, unambiguous source of truth for every piece of knowledge or logic in your codebase.

Dry Principle

Why Do We Need to Use DRY?

Using the DRY principle helps make your code more maintainable and less error-prone. When you have duplicate code, any change or bug fix needs to be applied in multiple places, increasing the risk of inconsistencies and errors. DRY ensures that you only need to make changes in one place, making your code easier to manage and understand.

How to Use the DRY Principle

Using Dry Principle

To apply DRY, look for repeated code or logic in your codebase and refactor it into a single, reusable component. This could mean creating functions, classes, or modules encapsulating repeated logic. Always aim to consolidate similar code to avoid duplication.


Conclusion

In the journey of software development, embracing the principles of YAGNI, DRY, and KISS can make a world of difference. By focusing on what's necessary with YAGNI, reducing repetition with DRY, and keeping things straightforward with KISS, you can create cleaner, more maintainable code. These principles not only help you write better software but also make your development process more efficient and enjoyable.

Clean Code Principle, YAGNI-KISS-DRY

Remember, the goal is to build software that is easy to understand, modify, and extend. By applying these principles, you set a strong foundation for your projects and make life easier for yourself and your team.

Stay tuned for our next article, where we'll dive into the SOLID principles, another set of guidelines that can help you become an even more effective software engineer. Happy coding!


Reference

  • Do check out Clean Code to understand more in-depth on the best practices.

  • Comic Strip made possible by people from Open Peeps

Top comments (22)

Collapse
 
bednic profile image
Tomas Benedikt

YAGNI does not make space for innovation and improvements. It seems like business focused. Which means only necessary things, defined outside of team will be realized. There should be always encouragement to do small cheap perks which improves product.
KISS principle works fine and by my observation is the most important. It helps with onboarding and should help with cognitive complexity.
DRY principle is very komplex and very often is in collision with KISS. Implement DRY the right way is hard and you need years of experience in software architecture, to recognize patterns and propose right abstraction. Sometimes two components which seems same are not same in core purpose and you will end up in same fuck ups like React framework.
So conclusion is, YAGNI should not stop programmers from doing nice cheap improvements in code. DRY is hard, even if it doesn't seems like that. KISS has always preference over DRY.

Collapse
 
juniourrau profile image
Ravin Rau

This is a thoughtful perspective on these principles! I completely agree that YAGNI can sometimes feel too business-focused and potentially stifle innovation if taken to the extreme. Encouraging small, low-cost improvements that enhance the product and codebase is essential for fostering creativity and maintaining developer morale.

Your observation about KISS being the most impactful resonates strongly. Keeping things simple not only aids onboarding but also reduces cognitive load, which is vital for maintaining productivity in the long run.

Regarding DRY, you're spot on about its complexity. Striking the right balance between abstraction and simplicity requires deep architectural insight, and as you mentioned, it can easily conflict with KISS. The React example is a great illustration of how over-generalizing can lead to unintended issues.

Ultimately, I agree with your conclusion: YAGNI should not discourage thoughtful improvements, and KISS should take precedence over DRY when conflicts arise. This balanced approach ensures practical, maintainable, and innovative development.

Collapse
 
codemouse92 profile image
Jason C. McDonald

Instead of YAGNI, I think about where we might want to take the product in the future, and then work backwards to ensure I'm not blocking any future development work. It's the idea that, you may not need that feature right this moment, but if you can make some simple design decisions now that make it possible, do it.

DRY is good to a point, but I've seen it get out of hands many times, producing code that is so abstracted it's virtually unmaintainable. The GCC compiler source (at least as of a few years ago) is a great example of overly DRY code.

Thread Thread
 
juniourrau profile image
Ravin Rau

Valid viewpoint, thank you very much for sharing this. Too much of something can sometimes be a bad thing. The right way is always to find that balance that enables you to be productive and efficient. At the end of the day, time is the most valuable thing, and being productive ensures that you can give the most output.

Collapse
 
lincpa profile image
Lin Pengcheng • Edited
Keep it Simple and Unified. 
Only simple can unity, 
only unity can be truly simple.

Traditional IT theory (OO, FP and hardware architecture, etc.) 
are pseudoscience. they belong to what physicist 
Wolfgang Pauli said "Not Even Wrong".

It is the perfect theory if a theory is based on mathematics 
and meets the five basic principles of "Simplicity, Unity, Order, 
Symmetry and Definiteness" in science, industry and aesthetics.  

Computer science is essentially a management science, 
and vice versa.

Software and hardware are factories that manufacture data, 
so they have the same "warehouse/workshop model" and 
management methods as the manufacturing industry.

        ---- Lin Pengcheng

The chief forms of beauty are order and symmetry and 
definiteness, which the mathematical sciences 
demonstrate in a special degree.
       ---- Aristotle, "Metaphysica"
Enter fullscreen mode Exit fullscreen mode

My programming aesthetic standards are derived from the basic principles of science. Newton, Einstein, Heisenberg, Aristotle and other major scientists hold this view.

The aesthetics of non-art subjects are often complicated and mysterious, making it difficult to understand and learn.

The pure function pipeline data flow provides a simple, clear, scientific and operable demonstration.

Simplicity and Unity are the two guiding principles of scientific research and industrial production.

  • Unification of theories is the long-standing goal of the natural sciences; and modern physics offers a spectacular paradigm of its achievement. It can be found from the knowledge of various disciplines: the more universally applicable a unified theory, the simpler it is, and the more basic it is, the greater it is.

  • The more simple and unified things, the more suitable for large-scale industrial production.

  • Only simple can unity, only unity can be truly simple.

In the IT field, only two systems fully comply with these 5 programming aesthetics:

  • Binary system

The biggest advantage is that it makes the calculations reach the ultimate simplicity and unity, so digital logic circuits are produced, and then the large-scale industrial production methods of computer hardware are produced.

  • The pure function pipeline data flow with principle-based warehouse/workshop model

From:

Collapse
 
juniourrau profile image
Ravin Rau

Thanks for sharing this perspective view. While I was reading this, the first thing that popped into my mind was the concept of the first principle. Start with a simple understanding/concept and slowly build from there. As you build, you optimize and aim to make things efficient. Continue and repeat the cycle.

Thanks for sharing your thoughts. ✨

Collapse
 
chad_fraser_757fa256b86dc profile image
Chad Fraser

"YAGNI, KISS, and DRY truly are the cornerstones of effective software development! 🌟 Adopting these principles not only simplifies the coding process but also ensures scalability and maintainability. A great reminder for both new and experienced developers to keep things clean and efficient! 💻✨"

Collapse
 
juniourrau profile image
Ravin Rau

Thank you very much @chad_fraser_757fa256b86dc

Collapse
 
rinidh profile image
Dhokia Rinidh

Excellent article bro. Complimenting on the YAGNI principle, I also think that out of many thousands of ideas/features we (in present day) may guess and add to our software, only one may hardly be accepted by users in the future. It's very hard to forecast the dynamic future. I guess that's why we should stick to current demands... YAGNI says it well 😉

Collapse
 
juniourrau profile image
Ravin Rau

Thank you very much @rinidhdhokia. You’re absolutely right! Businesses constantly adapt to meet the changing needs of their customers. Predicting customer preferences can be tricky, so it’s often safer to stick to what they’re already asking for. But that doesn’t mean we can’t innovate and find new ways to serve them better in the future!

Collapse
 
ifyour profile image
Willard Wong

Great article, it would be nice if each development principle was accompanied by practical examples! Thanks for sharing!

Collapse
 
juniourrau profile image
Ravin Rau

Thank you very much @ifyour. Sure I would dive deeper into the practical examples when I am focusing specifically on each of the principles in separate articles. This article covers more on the concept and understanding of the principles.

Collapse
 
newcoder789 profile image
Aryan Dixit

Nice small read🙏

Collapse
 
juniourrau profile image
Ravin Rau

Thank you very much @newcoder789 😁

Collapse
 
juliomoreyra profile image
JulioMoreyra

Clear and simple! Great

Collapse
 
juniourrau profile image
Ravin Rau

Thank you very much @juliomoreyra 😄

Collapse
 
robertolabarcab profile image
Roberto La Barca B.

Excelente artículo y que cumple con los 3 principios. Muchas gracias.

Collapse
 
juniourrau profile image
Ravin Rau

Muchas gracias por tus amables palabras! Me alegra saber que el artículo cumplió con tus expectativas y los principios. Tu comentario es muy valioso y motivador. 😊

Collapse
 
soleng profile image
na.perruchot

If only...

Collapse
 
juniourrau profile image
Ravin Rau

If only ?

Collapse
 
xmanhugo profile image
xmanhugo

Thanks for sharing, easy to understand

Collapse
 
juniourrau profile image
Ravin Rau

Thank you very much @xmanhugo. 😁

Some comments may only be visible to logged-in visitors. Sign in to view all comments.