The DRY Principle Is Misunderstood
May 2019 • Presentation
This presentation shows that the DRY Principle is really about eliminating duplicate representations of concepts in a system and offers techniques to find incorrectly DRY code.
Software Engineering Institute
The Don't Repeat Yourself Principle has swept the world. It is often the first thing new engineers are taught. It has probably affected your current code base in a big way. However, despite your code being "DRY," your code is hard to change. Files have to be changed for seemingly unrelated reasons as shotgun surgery and code brittleness abound. You might ask yourself, "Why is this happening?"
In this talk, I will show you that the DRY Principle actually is about eliminating duplicate representations of “concepts” in their system. Duplicate code is often misidentified as part of this principle, and attempts to clean it up are made. The end result is lots of shotgun surgery (many places change for the same reason) or divergent change (one place changes for many). This yields tons of pain for the development team, and eventually a clamor for a complete rewrite of the system.
I will give you three techniques to highlight incorrectly DRY code. These are
- focused conversations with the product team
- asking yourself the question "can I reason about this separately?" before you remove duplication
- how to notice shotgun surgery or divergent change
Once you know how to identify incorrectly DRY code, I will walk you through a code example where introducing a new path is very difficult. We will refactor the code so that the independent concepts are isolated. Once the code is fixed, I will show how easy it is to make the desired change—no extra conditions required.