Skip to content

Try to stay DRY!

Often times, when I teach iOS, I tell my students to try to overcome the very first principle of bad code – and that is, violation of the DRY principle. It stands for Don’t Repeat Yourself.

Idea is pretty simple: whenever you have a code which is duplicated, try to extract it. What’s the reason? Well first of all – this way, if you need to make a change, you will change it only in one place. That’s the most obvious reasons why we should try to follow this principle. But there are others as well. When the DRY principle is applied successfully, a modification of any single element of a system does not require a change in other logically unrelated elements.

Duplication can arise anywhere: in architecture, requirements, code, or documentation. The effects can range from bad code and developer confusion to a complete system failure.

How the principle gets violated

Well, one of the reasons is because we’re using copy and paste programming. We either copy our own code or, we grab some code snippet from some website directly into our solution, and there’s a good quote for that: ” Pasting code from the Internet into production code is like chewing gum found in the street. ” [Famous Quotes]

How not to get stuck in WET principle

WET principle stands for  “write everything twice”, “we enjoy typing” or “waste everyone’s time”. Generally, it’s the opposite of DRY principle. One of the good approaches with trying to avoid DRY violation is that when you find yourself writing code which is similar or equal to something you’ve written before, get away from the keyboard, take a moment to think about what you’re doing and simply – don’t repeat yourself.

One book that I would recommend, regarding the DRY principle, as well as, how to keep your code clean, is the Clean Code: A Handbook of Agile Software Craftsmanship.

You can see my short review of the book here.

Besides the book about Clean Code, this post is also about another great book – The Pragmatic Programmer: From Journeyman to Master by Dave Thomas, Andrew Hunt.

It was Dave Thomas who came up with the term ‘DRY’ in the first place.

In short – years of experience are packed into a book. You will see, as you start reading it, that most of the advice is timeless. In the book you will find an answers about what kind of specific coding practices should be carried out and which ones should be avoided.

You Can’t Write Perfect Software. Did that hurt? It shouldn’t. Accept it as an axiom of life. Embrace it. Celebrate it. Because perfect software doesn’t exist. No one in the brief history of computing has ever written a piece of perfect software. It’s unlikely that you’ll be the first. And unless you accept this as a fact, you’ll end up wasting time and energy chasing an impossible dream.

― Andy Hunt, Dave Thomas, The Pragmatic Programmer: From Journeyman to Master

Don’t leave “broken windows” (bad designs, wrong decisions, or poor code) unrepaired. Fix each one as soon as it is discovered. If there is insufficient time to fix it properly, then board it up. Perhaps you can comment out the offending code, or display a “Not Implemented” message, or substitute dummy data instead. Take some action to prevent further damage and to show that you’re on top of the situation.

― Andy Hunt, Dave Thomas, The Pragmatic Programmer: From Journeyman to Master

Every day, work to refine the skills you have and to add new tools to your repertoire.

― Andy Hunt, Dave Thomas, The Pragmatic Programmer: From Journeyman to Master

Finally, the DRY principle:

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

― Andy Hunt, Dave Thomas, The Pragmatic Programmer: From Journeyman to Master

Find this book. Read it. Become better. Have a nice day!

Published inBooks

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: