High Ruby

Prefer composition over inheritance

Rule Description

In Ruby, favor composition and modules over deep inheritance hierarchies:
- Use modules for shared behavior (concerns in Rails)
- Inject dependencies rather than inheriting from base classes
- Keep inheritance depth to 2-3 levels maximum
- Use duck typing - if it quacks like a duck, treat it like a duck

```ruby
# Good: Composition
class OrderProcessor
def initialize(payment_gateway:, notifier:)
@payment_gateway = payment_gateway
@notifier = notifier
end
end

# Avoid: Deep inheritance
class SpecialOrderProcessor < OrderProcessor < BaseProcessor < ApplicationService
```

Included in Rulesets

Rails Standards
36 rules • 1 standard
View
Ruby Style Guide
25 rules • 1 standard
View
Code Review Standards
58 rules • 0 standards
View

Add This Rule

Sign in to add this rule to your workspace

Sign in with GitHub

Details

Severity
High
Category
Ruby
Used in
3 rulesets