Importance of Writing DRY Code (Don’t Repeat Yourself).

Betty Bada
3 min readJul 14, 2020

--

You may not have heard it before but DRY code in software development means “Don’t Repeat Yourself”. According to Wikipedia DRY code is a principle of software development aimed at reducing the repetition of software patterns, replacing it with abstractions, or using data normalization to avoid redundancy.

In simpler terms, the DRY principle is a software development term that cuts across all programming languages and it has a singular purpose; to write code that can be used simultaneously.

Advantages of Writing DRY Code

Reusable Code: Developers need to write reusable code as this makes the work much easier. You don’t want to write thousands of lines of code that can’t be used for several files, you should endeavor to make your code universal.

Readable Code: You should always keep in mind that your code lives on, would the next person be able to understand what you’ve written? If the answer is no then you start implementing it. It should also be properly commented to explain the concepts.

Easy to Maintain: Good code must be easy to maintain as this saves a lot of time for the future users of your code. This is one of the main benefits of DRY code.

Cost: Having large code takes a lot of space thereby incurring more cost, more code means more time to maintain and handle bugs.

Testing: Clean code makes writing tests much easier, less code means less time spent testing.

Here’s an example of a DRY code:

Normally, we’d use console.log to log out the elements but we can use an array to log out repeated code. This makes the code shorter and easier to read.

Using ForLoop to execute repeated code

We can also write clean code in CSS, by adding elements with the same styling together.

How to write DRY Code in CSS

DRY code takes much time to write as the code needs to be organized, commented properly to prevent errors, and also to make sure the code is easy to maintain over time.

It is worthy of note that not all code needs to be merged as some code may have subtle differences between them. Over dried code, therefore, becomes hard to read and understand inevitably making the whole process useless. DRY helps identify edge cases where your code will need to do different things in ways that might not be obvious.

--

--

Betty Bada
Betty Bada

Written by Betty Bada

Product Manager with a flair for writing

No responses yet