---
title: Don't Repeat Yourself (DRY)
impact: MEDIUM
impactDescription: Reduce code duplication and improve maintainability.
tags: ruby, abstraction, maintenance, quality
---

## Don't Repeat Yourself (DRY)

Reduce code duplication and improve maintainability. Extract repeated code into methods, helpers, concerns, or partials. Use inheritance or modules to share behavior instead of copy-pasting code blocks.

**Incorrect:**

```ruby
def create_admin
  user = User.new(admin: true)
  user.save!
  Log.create(action: "admin created")
end

def create_user
  user = User.new(admin: false)
  user.save!
  Log.create(action: "user created")
end
```

**Correct:**

```ruby
def create_account(admin: false)
  user = User.create!(admin: admin)
  Log.create(action: "#{admin ? 'admin' : 'user'} created")
end
```

**Tools:** Reek, RuboCop (`Style/IdenticalConditionalBranches`)
