KEEP YOUR CODE DRY

How to not write the same code twice, because YOLO doesn't work in real life.

DRYCODE.IO

Committed to duplicate free code

BACK TO TOP
Writing the same code twice

Don’t Repeat Yourself is a basic principle is software design and programming. It basically means you shouldn’t duplicate or rewrite the same functionality twice. Instead, you should turn to abstraction in order to avoid redundancies which creates a serious maintenance problem and technological debt over time. 

Here is the most painful problem with duplicating code in simple words: If you find a bug in your duplicated code, you will need to remember and fix it every place you implemented it in. If you keep logic in one place, it is easier to change when needed.

When approaching the practical workflow around DRY code it’s important to understand one thing: copy pastes and duplicates are merely a symptom. They are not the problem's root cause.


What is DRY code and why it’s so important

Duplicate code on stack overflow

Design abstract components which are built for modularity. Addy Osmani described the FIRST principle which states that components should be FOCUSED, INDEPENDENT (or encapsulated), REUSABLE, SMALL and TESTABLE.


How to keep your code DRY

Tools like Bit can help organize and share smaller components between projects with the efficiency of copy-pasting, and still keep them managed without the overhead of multiple packages.  


Here are a few useful bits of advice on how to avoid duplicate code.

Write unit-tests and documents smaller components to increase reusability.

Keep a Clear, understandable and obvious code structure and architecture.

Code reviews can help take an objective second look at how your code is built and identify duplicates and rewrites.

Enforcement is a bad word, but it helps.

DRY code is key

Copy pastes are only a symptom. 

They are not the problem’s cause.

They won’t go away if you will not address the real problem- which is the reason they are created. All the enforcement and good intentions will probably eventually fail unless the core cause is addressed. And here is the problem:

To prevent copy-pasting we need a fast and simple experience for sharing code between projects and managing changes.

Think about music CD-Roms for a second. Almost everyone has duplicated the same songs between different CDs. Why? Because they didn’t care? No. It happened because they wanted to consume the same song while listening to 2 different CDs, and had no better way to dynamically arrange a playlist. 

As most experienced developers understand the implications of duplicate code, it is crucial to provide the right experience, tooling and culture to do better. We have to provide people with a practical way to avoid copy-pastes and also benefit from doing so in their experience. We’ll get to that later.


Some developers will still need to be convinced though...

Write the same code

How to detect duplicate code

First, you would have to find and locate the duplicate code. Note that detecting re-implementations (which are different in sytax) is much more complicated with traditional methods, and can be done with tools like Bit (see below). Changing the different  implementations is also more complicated as each one is different and may also require different external or global assets from within or from outside the project.

Detecting copy-pastes can be done with different tools (some are open source and can be installed using package managers).

Detecting both copy-pastes and re-implementations for can be done by turning the shared source code pieces into managed shared components with Bit. It enables you to isolate and share components of source code from any repository and organize them in a discoverable collection, where you can easily learn which pieces of code had been implemented or duplicated more than once, and where.

Code duplications

How to get rid of duplicate code

First, you have to want to get rid of it. That might sound simple, bit it also means you have to act to prevent and fix this issue.

Then, you have to decide the scope of projects you wish to care for.

The next step is choosing the right tooling. And here is the important part: you have to choose a tool that will help solve the core problem, not the symptom. For example, if you wish to stop duplicating songs between music CD-Roms then using a mini-disc or rearranging our CDs won’t really solve the problem. You have to change the way you share, organize and consume music. This is why iTunes managed to change the world.

To build that iTunes for small pieces of shared code you can use Bit which enables you to easily share small components with the speed of copy-pasting and with the advantages of keeping them managed. You can create dynamic collections of shared components, use them from any project and keep them synced with cross-project updates and tracked changes to easily change multiple components in multiple projects with a single source of truth (iTunes for shared code, if you will).

Duplicate code and microservices

Share code between microservices

The nature of microservices is to seperate work and concerns on different parts of your architecture through different smaller source code projects. 

As such, much of the code needed in these different services is, in fact, the same code. Manu services often include contact points with other serices, or common encapsulated logics which are required to full fill the goals of multiple services.

As a result, code is often being duplicated between these services. Using coupling-free code sharing methods, which help to manage and sync common code without coupling it to 3rd party shared-libs, you can avoid duplicates and build your architecture using managed shared building blocks. LEARN MORE

Duplicate code and monorepos

Terms of use
mono repo and duplicate code

To avoid the overhead of maintaining duplicate code or making changes to packages across multiple repositoires, some teams choose to keep their code and packages in a single repository called a "monorepo". However, it is still vital to share and sync common code between these monorepos. LEARN MORE

Learn more about the DRY code principle