---
title: Use guard clauses for early returns
impact: MEDIUM
impactDescription: Reduce nesting and improve code clarity.
tags: ruby, quality, style, clean-code
---

## Use guard clauses for early returns

Reduce nesting and improve code clarity. Use guard clauses to handle edge cases or invalid states early and return. This keeps the main happy path at the top level of the method.

**Incorrect:**

```ruby
def notify(user)
  if user.active?
    if user.email_notifications?
      # Main logic here
      UserMailer.notification(user).deliver_now
    end
  end
end
```

**Correct:**

```ruby
def notify(user)
  return unless user.active?
  return unless user.email_notifications?

  # Main logic here
  UserMailer.notification(user).deliver_now
end
```

**Tools:** RuboCop (`Style/GuardClause`)
