DRY: Don't Repeat Yourself
Posted on January 23, 2024 • 4 min read • 739 words
While I was recently immersed in various Hugo-related tasks, I stumbled upon the abbreviation DRY. I had to look that one up for sure. DRY stands for Don’t Repeat Yourself, which basically means what it says — don’t repeat yourself.
Don’t Repeat Yourself
The principle of DRY goes like this: “Every piece of knowledge should have a single, unambiguous, authoritative representation within a system.” That’s quite a mouthful. DRY originates from The Pragmatic Programmer, a book about software development. Looking back, I do recognize the method. During my education, a long time ago, I used this approach. One of the things I applied it to was database normalization. Ultimately, information is stored only once, and you refer to the data whenever needed.
For example: A sales registration system needs to store certain data for a sale. After all, you want to know what the customer has bought, where the product should be sent, and what the cost was. You could use an Excel sheet, where every line contains all the information.
| product | price | customer name | street + house number | postal code | city |
|---|---|---|---|---|---|
| bicycle bell | 3.99 | Johnny Doe | Cycle Square 1 | SW1A 1AA | London |
| valve cap | 0.95 | Johnny Doe | Cycle Square 1 | SW1A 1AA | London |
| handlebar | 15.49 | Johnny Doe | Cycle Square 1 | SW1A 1AA | London |
You probably see where this is going. Just in these three lines, there’s already the same information listed three times. How convenient would it be if you had a table with customer details that you could refer to in the sales table.
Customer Table:
| customerID | Name | street + house number | Postal Code | City |
|---|---|---|---|---|
| 1 | Johnny Doe | Cycle Square 1 | SW1A 1AA | London |
Sales Table:
| customerID | product | price |
|---|---|---|
| 1 | bicycle bell | 3.99 |
| 1 | valve cap | 0.95 |
| 1 | handlebar | 15.49 |
Besides saving keystrokes and possibly storage space, another advantage emerges. Since you’ve only stored the data once, it is also easier to modify. Suppose the customer moves; then the data only needs to be changed in one place. This prevents unnecessary typing but, above all, errors.
The same principle applies to many things. It’s better to program or build something properly once and then reuse it, rather than reinventing the wheel each time.
You could say that it’s a method for lazy people, or rather, smart people. Why do things more often than necessary if you can automate them? The advantage is that they’re then performed without errors and always in the same manner—assuming you’ve automated it correctly, otherwise, it will go wrong in the same way every time.
Not Just for Programming
DRY comes from the developer’s world, but I believe it can be applied philosophically in other areas as well. Why do something twice if you can do it right once? Yes, you might spend a bit more time on what you need to do, but the next time it will only take a fraction of the time, plus you can predict the outcome.
I asked my buddy AI (ChatGPT) about the benefits of DRY for programming. Be gentle.. This is not my communication style :)
- Efficiency: Not repeating code means you write less, leading to faster development.
- Maintainability: When a piece of logic needs to be changed, it only has to be done in one place. This minimizes the risk of inconsistencies and errors.
- Readability: A clean codebase without repetitions is easier for new developers to understand when working on a project.
- Testability: Code that’s located in one place is simpler to test and validate.
- Flexibility and Scalability: Systems based on the DRY principle are easier to adapt to changes and new functionalities.
They make for great exam questions, if you ask me. It makes sense: If you only write/program things once, then you only need to look in one place when troubleshooting. And readability can increase, as long as you program it neatly the one time you are building it.
But also for your ‘regular’ life, outside of programming, it’s quite a handy method. Once you’ve thought of something, you can describe or store it somewhere for reuse. Why reinvent the wheel?
I don’t want to turn this into an advertisement for Obsidian , but you could use it to store your gained knowledge and experience.
Now It’s Your Turn
Were you already familiar with the DRY method, or do you find it as logical as I do and don’t need to think much about it? Let me know below.