DRY — Don’t Repeat Yourself: The Golden Rule of Clean Code

Pankaj Jangid
3 min readAug 28, 2024

--

When it comes to software development, there’s a mantra that every developer should tattoo on their brain: DRY — Don’t Repeat Yourself. It’s a principle that’s as essential to coding as coffee is to developers. But why is this rule so important, and how can we apply it effectively in our code? Let’s dive in and explore the world of DRY in a fun and insightful way.

What is DRY?

The DRY principle, coined by Andy Hunt and Dave Thomas in their book The Pragmatic Programmer, is a simple yet powerful concept. The idea is to reduce the repetition of code patterns, knowledge, and logic to avoid redundancy. DRY code is easier to maintain, more robust, and less prone to errors.

Imagine you’re cooking a recipe, and every time you need to add salt, you measure it out individually instead of just using a teaspoon. That’s repetitive and inefficient — just like copy-pasting code across your project. DRY says: measure once, use multiple times.

Why DRY is Essential

1. Maintainability

When your code isn’t DRY, you’re setting yourself up for a maintenance nightmare. If a change is required, you have to modify it in several places, increasing the risk of missing a spot and introducing bugs.

2. Readability

DRY code is easier to read and understand. It focuses on making each piece of knowledge explicit, with no duplicated logic to puzzle over.

3. Reduced Risk of Errors

The more you repeat code, the higher the chances of inconsistency and errors. DRY helps mitigate this by centralizing logic, so you only need to update it in one place.

DRY in Action: A Real-Life Analogy

Think of DRY as preparing for a big presentation. You wouldn’t create individual slides for every instance you want to mention a key point; instead, you’d use one well-crafted slide and refer back to it whenever needed. Similarly, in your code, you should create a single, reusable function or module for any logic that repeats.

Applying DRY: A Practical Example

Let’s consider an example in Android development. Suppose we have two activities that both fetch user data from an API and display it in a list. Here’s how it might look without applying DRY:

class FirstActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Fetch data for FirstActivity
fetchUserData()
}

private fun fetchUserData() {
// Network request to fetch user data
// Populate list
}
}
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Fetch data for SecondActivity
fetchUserData()
}
private fun fetchUserData() {
// Network request to fetch user data
// Populate list
}
}

Both activities perform the same task, but the code is repeated. Now, let’s refactor this to apply the DRY principle:

abstract class BaseActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
fetchUserData()
}

protected fun fetchUserData() {
// Network request to fetch user data
// Populate list
}
}
class FirstActivity : BaseActivity() {
// No need to override fetchUserData, logic is centralized in BaseActivity
}
class SecondActivity : BaseActivity() {
// No need to override fetchUserData, logic is centralized in BaseActivity
}

By creating a BaseActivity, we centralize the logic for fetching user data, eliminating repetition and making our code more maintainable and easier to understand.

When NOT to Apply DRY

While DRY is a powerful principle, it’s not a silver bullet. Overzealous application of DRY can lead to complex and abstract code that’s hard to understand. For instance, trying to eliminate every bit of repetition may result in premature optimization or lead to convoluted inheritance structures. Always balance DRY with readability and simplicity.

Conclusion

In software development, DRY is more than just a guideline; it’s a philosophy that drives clean, maintainable, and efficient code. By avoiding redundancy, we not only make our codebase more manageable but also reduce the risk of errors and improve readability. Remember, the next time you’re tempted to copy-paste that block of code — stop and think: “Can I DRY this up?”

References

  1. The Pragmatic Programmer: Your Journey to Mastery
  2. Understanding the DRY Principle in Software Development
  3. DRY Principle Explained with Examples
  4. When Not to Apply DRY: The WET Codebase
  5. Why DRY is Important for Clean Code

--

--

No responses yet