Blog

Why is Code Reuse Important?

Gavin Pickin May 02, 2022

Spread the word

Gavin Pickin

May 02, 2022

Spread the word


Share your thoughts

I know we all reuse code and some types of reuse are better than the others (I'm looking at you COPY AND PASTE / man in the mirror), but copy-paste is only a symptom of a larger issue, that WET code is not as easy to maintain as DRY code.

What is DRY Code?

Don't Repeat Yourself (DRY) code aims to reduce code repetition. We do not want to reinvent the wheel, but worse is having bad versions of the wheel scattered everywhere. The goal of DRY code is to refactor so that you only have one reusable instance of that code, and if you need to change it, you can do so in one place. It is easier to maintain and update. DRY code also reduces the chance of error since you are only updating one place instead of several. We have all used DRY code when we have used loops, functions, or includes.

What is WET code?

Write Everything Twice (WET) code is the opposite of DRY code. It is where you have duplicate code copy/pasted, or rewritten several times in multiple places. Sometimes WET code is DRY code that needs minor changes, but you make a copy of it. If it is a simple change, it might be ok to add it to your DRY code, but sometimes it's better not to over-engineer things. DRY code is the goal in most cases, and the benefits will outweigh the costs.

Why do we want DRY Code?

The biggest benefit of using DRY code is maintainability. Fixing bugs in one location is easier than remembering and finding all the occurrences of that logic. Likewise, updating the business logic or adding functionality is easier when the code is shared.

An Acronym used with DRY code is FIRST, which stands for Focused, Independent, Reusable, Small, and Testable (https://addyosmani.com/first/)

Readability

Programs must be written for people to read, and only incidentally for machines to execute - Harold Abelson

If a developer understands the principles of DRY code, they are more likely to understand clean code principles, leading to more readable code. Code reuse usually encourages smaller, more manageable functions, promoting lower cognitive load and making the code more readable.

Testing

DRY code is broken down into smaller pieces and is usually easier to test. The more code you have and the more paths and functions to cover, the harder it will be to test. Using DRY Code from libraries and frameworks means more eyes on it, and sometimes those libraries and frameworks have tests for their code already.

Cost

  • More code takes more time and money
  • Reinventing the wheel takes more time and money
  • Maintaining more takes costs more time and money
  • Testing more code takes more time and money
  • Fixing more bugs takes more time and money

How do we prevent Copy Pasting and WET Code?

We need fast and simple ways to share code in a project and between projects. We are lucky in ColdFusion in that we have lots of techniques and tools to help us write DRYer code. Check back for our next blog entry, where we go into more detail.

Add Your Comment

(1)

May 09, 2022 15:51:00 UTC

by Jason

Thanks for this Gavin. Tryng to convince other members in our crew to adopt this philosophy is somewhat challenging.

Recent Entries

BoxLang 1.0.0 Beta 23 Launched

BoxLang 1.0.0 Beta 23 Launched

The latest release of BoxLang, Beta 23, marks a significant step forward in our journey to create the ultimate dynamic language for the JVM. Packed with powerful new features, important bug fixes, and thoughtful optimizations, this update is designed to make your development experience smoother, faster, and more reliable, especially after now starting to take 100s of comments and bug reports from our community.

Luis Majano
Luis Majano
November 23, 2024
TestBox Latest Updates and News!

TestBox Latest Updates and News!

We’re thrilled to have launched the new TestBox website and TestBox 6.0! If you haven’t had a chance to explore yet, visit TestBox to discover updated documentation, powerful resources, and features that make testing more efficient than ever.

Maria Jose Herrera
Maria Jose Herrera
November 21, 2024
Is Your ColdFusion Application Ready for the Future?

Is Your ColdFusion Application Ready for the Future?

In a rapidly evolving digital world, maintaining performance, security, and scalability for ColdFusion applications is more challenging than ever. Whether you're using Lucee or Adobe ColdFusion, legacy systems can become a bottleneck for growth, innovation, and user satisfaction. The need to future-proof your ColdFusion applications has never been more critical.

But where do you start?


The Hidden Costs of an Outdated ColdFusion Application

As you...

Cristobal Escobar
Cristobal Escobar
November 21, 2024