DEV Community

Cover image for 12 Steps to Staying DRY

Posted on

12 Steps to Staying DRY

When you are addicted to your spaghetti code and you are getting tipsy on the next line of instant gratification, remember to follow sobriety and stay DRY - Femi's Nuggets.

The urge to keep writing codes that are not reusable can be compelling and in actual fact become an addiction. Just like most addiction, there is a feeling that comes with copying and pasting code that increases the dopamine level. I was a victim of this addiction too. Coming from the world of vanilla PHP and Perl to build web applications where you are not mandated to follow modern design patterns or conventions or paradigms made me engrossed in spaghettism. Spaghettism 🍝 is a topic for another day 😊.

To move from this addictive stage can be tough. Real tough! But trust me you can do it if you follow these 12 steps to staying DRY.

Note: DRY means Don't Repeat Yourself.

  1. Admit that writing non-reusable code is wicked to both yourself and every stakeholder: You need to just admit it, Gee. Think about the effect of repeating yourself on your career's health. Think about the cost of employing someone else to maintain your source code. Mehn! It’s scary, right😨?

  2. Find greater power in data structures: You would be surprised what data structures can do to you. Arrays can eliminate nested branching or decision statements, dictionaries can limit nested iterations, and much more. You can also use trees. But don’t get addicted to trees too😊. Learn more about data structures and their applications.

  3. Study fervently more design patterns and paradigm best-practices: Learn SOLID for OOP. Learn more best-practices for the programming paradigm you choose. Master it. Be familiar with what and when to use some design patterns such as singleton, DAO, active records, etc. Use them more often.

  4. Study source code of standard open-source systems and implement some DRY practices you notice: Open-source systems usually have a bunch of good heads coming together to build something. Try to learn some of the DRY practices used yourself by studying the source code.

  5. Calculate often the time complexity of your block of codes: Use the big O notation to calculate the worst-case running time of your block of codes and eliminate the not-so-good Os such as Oh no! Oh shit!

  6. Avoid copying and pasting or break your Ctrl key of your keyboard completely😊: Please don’t break your ctrl key if you are going to use it for other things. But come to think of it, DRY should actually be a way of life. Unpredictability, creativity, new vibes, and other contemporary attractive human behavior are things you get from not repeating yourself. Aren’t they?

  7. Humbly devote yourself to algorithm study: Learn algorithms daily (even if it is 10 minutes daily) and apply them to your code writing. It can be boring as hell but it could be hell if you don’t learn them either.

  8. Make a list of projects you have worked on with much non-reusable code: You need to do this. To know how far you have gone from being DRY. This is a giant step to sobriety.

  9. Make direct amends to such projects if possible: Ask people that have maintained your past non-reusable code for forgiveness. You have probably hurt them deeply. Let them know of your new ways and explain to them how they can mitigate some of the spaghettism with the new knowledge you have.

  10. Continue to practice writing more DRY codes comparing it with standard open-source systems that follow the concept: Just keep writing codes that are DRY even if it is a function that involves adding just two numbers. Compare your approach with what is out there.

  11. Sought peer review of your newly written codes with the DRY concept: Talk to your DRY friends. Have daily stand up over some cup of coffee on understanding each other’s codes and if there are too many non-DRY codes and you can’t stand it, then have a sit-down with the culprit. Make sure you don’t see evil non-DRY codes and keep quiet because that would make you only evil as well.

  12. Having gone through these steps, preach your new-found knowledge to people: You can do this by writing on blogs, speaking where you have the opportunity, and programming by example. Also, constantly think about your own creative DRY methods and sell the idea to the public.

In conclusion:

Programming is a serious business, Mr. Dry; it has been since man invented the first computer. Our children will live, Mr. Dry, to see that perfect world in which there's no non-reusable code, coding aches, or maintenance phobia - one vast and ecumenical community of DRY developers, for whom all programmers will work to serve a common profit, in which all programmers will hold a share of stock - all necessities provided, all anxieties tranquilized, all boredom amused. And I have chosen you, Mr. Not Dry, to preach this evangel.

Have a DRY October ahead!

Top comments (0)