---
title: Document callbacks and use them sparingly
impact: MEDIUM
impactDescription: Prevent hidden side effects and improve code clarity.
tags: rails, architecture, callbacks, documentation
---

## Document callbacks and use them sparingly

Prevent hidden side effects and improve code clarity. Use callbacks (like `before_save`, `after_create`) only when necessary. Consider using service objects if a callback triggers complex business logic.

**Incorrect (Hidden side effects):**

```ruby
class Order < ApplicationRecord
  after_create :charge_credit_card # Dangerous side effect hidden in model lifecycle
end
```

**Correct (Explicit action):**

```ruby
class CheckoutService
  def self.call(order)
    order.save!
    PaymentGateway.charge(order) # Explicit and easier to test
  end
end
```

**Tools:** Manual Review
