Early returns in Ruby

Use early returns to reduce nesting levels. Prefer checking error conditions or guard clauses first and returning early, rather than wrapping the main logic in deep conditional blocks.
Bad
```ruby
def process_data(data)
  if data.present?
    if valid?(data)
      result = ""

Install this rule for wispbit

Add this rule to wispbit and it will run when you open a pull request

Install this rule for Coderabbit

Copy the configuration below and add it to your repository as .coderabbit.yml in your project root.

reviews:
  path_instructions:
    - path: "*.rb"
      instructions: |
                
        Use early returns to reduce nesting levels. Prefer checking error conditions or guard clauses first and returning early, rather than wrapping the main logic in deep conditional blocks.
        
        Bad
        
        ```ruby
        def process_data(data)
          if data.present?
            if valid?(data)
              result = ""
              data.each do |item|
                if item.present?
                  # More nested code here
                  result += transform(item)
                end
              end
              { success: result }
            else
              { error: "invalid data" }
            end
          else
            { error: "empty data" }
          end
        end
        ```
        
        Good
        
        ```ruby
        def process_data(data)
          return { error: "empty data" } if data.empty?
          return { error: "invalid data" } unless valid?(data)
        
          result = ""
          data.each do |item|
            next if item.blank?
            result += transform(item)
          end
        
          { success: result }
        end
        ```
        

Install this rule for Greptile

Greptile rules can be added through the web interface. Please see this documentation for details on how to add custom rules and context.

Use early returns to reduce nesting levels. Prefer checking error conditions or guard clauses first and returning early, rather than wrapping the main logic in deep conditional blocks.
Bad
```ruby
def process_data(data)
  if data.present?
    if valid?(data)
      result = ""
      data.each do |item|
        if item.present?
          # More nested code here
          result += transform(item)
        end
      end
      { success: result }
    else
      { error: "invalid data" }
    end
  else
    { error: "empty data" }
  end
end
```
Good
```ruby
def process_data(data)
  return { error: "empty data" } if data.empty?
  return { error: "invalid data" } unless valid?(data)
  result = ""
  data.each do |item|
    next if item.blank?
    result += transform(item)
  end
  { success: result }
end
```

File Path Patterns:

*.rb

Install this rule for GitHub Copilot

Copilot instructions can be added through the interface. See the documentation for details on how to create coding guidelines.

Use early returns to reduce nesting levels. Prefer checking error conditions or guard clauses first and returning early, rather than wrapping the main logic in deep conditional blocks.
Bad
```ruby
def process_data(data)
  if data.present?
    if valid?(data)
      result = ""
      data.each do |item|
        if item.present?
          # More nested code here
          result += transform(item)
        end
      end
      { success: result }
    else
      { error: "invalid data" }
    end
  else
    { error: "empty data" }
  end
end
```
Good
```ruby
def process_data(data)
  return { error: "empty data" } if data.empty?
  return { error: "invalid data" } unless valid?(data)
  result = ""
  data.each do |item|
    next if item.blank?
    result += transform(item)
  end
  { success: result }
end
```

File Path Patterns:

*.rb

Install this rule for Graphite Diamond

Diamond custom rules can be added through the interface. See the documentation for details on how to create custom rules.

Use early returns to reduce nesting levels. Prefer checking error conditions or guard clauses first and returning early, rather than wrapping the main logic in deep conditional blocks.
Bad
```ruby
def process_data(data)
  if data.present?
    if valid?(data)
      result = ""
      data.each do |item|
        if item.present?
          # More nested code here
          result += transform(item)
        end
      end
      { success: result }
    else
      { error: "invalid data" }
    end
  else
    { error: "empty data" }
  end
end
```
Good
```ruby
def process_data(data)
  return { error: "empty data" } if data.empty?
  return { error: "invalid data" } unless valid?(data)
  result = ""
  data.each do |item|
    next if item.blank?
    result += transform(item)
  end
  { success: result }
end
```

File Path Patterns:

*.rb

Use with Cline

Copy the rule below and ask Cline to review your code using this rule

Use early returns to reduce nesting levels. Prefer checking error conditions or guard clauses first and returning early, rather than wrapping the main logic in deep conditional blocks.
Bad
```ruby
def process_data(data)
  if data.present?
    if valid?(data)
      result = ""
      data.each do |item|
        if item.present?
          # More nested code here
          result += transform(item)
        end
      end
      { success: result }
    else
      { error: "invalid data" }
    end
  else
    { error: "empty data" }
  end
end
```
Good
```ruby
def process_data(data)
  return { error: "empty data" } if data.empty?
  return { error: "invalid data" } unless valid?(data)
  result = ""
  data.each do |item|
    next if item.blank?
    result += transform(item)
  end
  { success: result }
end
```

Use with OpenAI Codex

Copy the rule below and ask OpenAI Codex to review your code using this rule

Use early returns to reduce nesting levels. Prefer checking error conditions or guard clauses first and returning early, rather than wrapping the main logic in deep conditional blocks.
Bad
```ruby
def process_data(data)
  if data.present?
    if valid?(data)
      result = ""
      data.each do |item|
        if item.present?
          # More nested code here
          result += transform(item)
        end
      end
      { success: result }
    else
      { error: "invalid data" }
    end
  else
    { error: "empty data" }
  end
end
```
Good
```ruby
def process_data(data)
  return { error: "empty data" } if data.empty?
  return { error: "invalid data" } unless valid?(data)
  result = ""
  data.each do |item|
    next if item.blank?
    result += transform(item)
  end
  { success: result }
end
```

Use with Cursor

Copy the rule below and ask Cursor to review your code using this rule

Use early returns to reduce nesting levels. Prefer checking error conditions or guard clauses first and returning early, rather than wrapping the main logic in deep conditional blocks.
Bad
```ruby
def process_data(data)
  if data.present?
    if valid?(data)
      result = ""
      data.each do |item|
        if item.present?
          # More nested code here
          result += transform(item)
        end
      end
      { success: result }
    else
      { error: "invalid data" }
    end
  else
    { error: "empty data" }
  end
end
```
Good
```ruby
def process_data(data)
  return { error: "empty data" } if data.empty?
  return { error: "invalid data" } unless valid?(data)
  result = ""
  data.each do |item|
    next if item.blank?
    result += transform(item)
  end
  { success: result }
end
```

Use with Claude Code

Copy the rule below and ask Claude Code to review your code using this rule

Use early returns to reduce nesting levels. Prefer checking error conditions or guard clauses first and returning early, rather than wrapping the main logic in deep conditional blocks.
Bad
```ruby
def process_data(data)
  if data.present?
    if valid?(data)
      result = ""
      data.each do |item|
        if item.present?
          # More nested code here
          result += transform(item)
        end
      end
      { success: result }
    else
      { error: "invalid data" }
    end
  else
    { error: "empty data" }
  end
end
```
Good
```ruby
def process_data(data)
  return { error: "empty data" } if data.empty?
  return { error: "invalid data" } unless valid?(data)
  result = ""
  data.each do |item|
    next if item.blank?
    result += transform(item)
  end
  { success: result }
end
```

Install this rule for Windsurf

To set up rules for Windsurf Reviews, please see this documentation

Use early returns to reduce nesting levels. Prefer checking error conditions or guard clauses first and returning early, rather than wrapping the main logic in deep conditional blocks.
Bad
```ruby
def process_data(data)
  if data.present?
    if valid?(data)
      result = ""
      data.each do |item|
        if item.present?
          # More nested code here
          result += transform(item)
        end
      end
      { success: result }
    else
      { error: "invalid data" }
    end
  else
    { error: "empty data" }
  end
end
```
Good
```ruby
def process_data(data)
  return { error: "empty data" } if data.empty?
  return { error: "invalid data" } unless valid?(data)
  result = ""
  data.each do |item|
    next if item.blank?
    result += transform(item)
  end
  { success: result }
end
```