Ruby Refactoring Expert by ag0os/rails-dev-plugin
npx skills add https://github.com/ag0os/rails-dev-plugin --skill 'Ruby Refactoring Expert'运用《Ruby Science》中的原则和成熟的重构模式,进行系统性的代码改进。
系统地扫描反模式和问题代码结构。完整目录请参阅 code-smells.md。
常见代码异味:
根据对可维护性、性能和业务价值的影响对问题进行排序。
优先级级别:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
首先关注高影响、低风险的重构。
针对每个问题,提出具体的重构模式并附上具体示例。
完整模式目录请参阅 refactoring-patterns.md。
保持务实:
记住:完美的代码不如团队能够理解的、可维护的、能工作的代码重要。
在重构之前:
适用场景:方法 > 10-15 行或承担多项职责
# Before
def calculate_total
subtotal = line_items.sum(&:amount)
tax = subtotal * tax_rate
shipping = calculate_shipping(subtotal)
subtotal + tax + shipping
end
# After
def calculate_total
subtotal + tax + shipping_cost
end
private
def subtotal
line_items.sum(&:amount)
end
def tax
subtotal * tax_rate
end
def shipping_cost
calculate_shipping(subtotal)
end
适用场景:类 > 100 行或承担多重职责
# Before: User class handling authentication AND profile management
class User < ApplicationRecord
def authenticate(password)
# Authentication logic
end
def update_profile(params)
# Profile logic
end
def send_welcome_email
# Email logic
end
end
# After: Separated concerns
class User < ApplicationRecord
has_one :user_profile
def authenticate(password)
# Authentication only
end
end
class UserProfile < ApplicationRecord
belongs_to :user
def update(params)
# Profile management
end
end
class UserNotifier
def self.send_welcome(user)
# Email logic
end
end
适用场景:逻辑涉及多个模型或具有复杂的编排
# Before: Fat controller or model method
class PolicyRenewalService
def initialize(policy, new_expiry_date)
@policy = policy
@new_expiry_date = new_expiry_date
end
def call
return failure("Not renewable") unless @policy.renewable?
ApplicationRecord.transaction do
archive_old_policy
update_policy
create_invoice
send_notifications
end
success(@policy)
rescue StandardError => e
failure(e.message)
end
end
适用场景:基于类型的复杂条件判断
# Before: Type checking
def calculate_premium
case insurance_type
when 'auto'
base_rate * vehicle_factor * driver_age_factor
when 'home'
base_rate * property_value_factor * location_risk
when 'life'
base_rate * age_factor * health_factor
end
end
# After: Polymorphism
class AutoInsurancePolicy < Insurance::Policy
def calculate_premium
base_rate * vehicle_factor * driver_age_factor
end
end
class HomeInsurancePolicy < Insurance::Policy
def calculate_premium
base_rate * property_value_factor * location_risk
end
end
适用场景:方法有 > 3 个参数或参数组频繁一起出现
# Before
def create_policy(policy_number, effective_date, expiry_date, premium, person_id, company_id)
# ...
end
# After
class PolicyAttributes
attr_reader :policy_number, :effective_date, :expiry_date,
:premium, :person_id, :company_id
def initialize(params)
@policy_number = params[:policy_number]
@effective_date = params[:effective_date]
# ...
end
def valid?
# Validation logic
end
end
def create_policy(attributes)
return unless attributes.valid?
# ...
end
使用代码块和枚举器:
# ✅ Good
users.select(&:active?).map(&:email)
# ❌ Bad
result = []
users.each do |user|
result << user.email if user.active?
end
使用符号作为键:
# ✅ Good
{ name: 'John', age: 30 }
# ❌ Bad
{ 'name' => 'John', 'age' => 30 }
精简控制器,专注模型:
使用作用域进行查询:
# ✅ Good
class Policy < ApplicationRecord
scope :active, -> { where(status: 'active') }
scope :expiring_soon, -> { where('expiry_date < ?', 30.days.from_now) }
end
# ❌ Bad
def self.active_policies
where(status: 'active')
end
提供重构建议时:
列出已识别的问题,包括严重程度(高/中/低)和位置
按优先级排序的重构步骤列表
具体的重构前/后代码示例
所需的测试变更或补充
如何安全地部署变更
在最终确定建议前:
| 异味 | 模式 | 适用场景 |
|---|---|---|
| 长方法 | 提取方法 | 方法 > 10-15 行 |
| 大类 | 提取类 | 类 > 100 行 |
| 长参数列表 | 参数对象 | > 3 个参数 |
| 特性依恋 | 移动方法 | 使用其他对象的数据 |
| 基本类型偏执 | 提取值对象 | 带有行为的基本类型 |
| 复杂条件表达式 | 多态 | 基于类型的条件判断 |
| 重复代码 | 提取方法/模块 | 相同代码出现在 2 个以上地方 |
在不确定时询问:
记住:目标是团队能够理解的可维护代码,而不是完美的代码。
每周安装量
0
代码仓库
GitHub 星标数
4
首次出现
1970年1月1日
安全审计
Systematic code improvement using principles from Ruby Science and established refactoring patterns.
Systematically scan for anti-patterns and problematic code structures. See code-smells.md for complete catalog.
Common code smells :
Rank problems by impact on maintainability, performance, and business value.
Priority levels :
Focus on high-impact, low-risk refactorings first.
For each issue, suggest specific refactoring patterns with concrete examples.
See refactoring-patterns.md for complete pattern catalog.
Be pragmatic:
Remember : Perfect code is less important than working, maintainable code the team can understand.
Before refactoring:
When : Method > 10-15 lines or does multiple things
# Before
def calculate_total
subtotal = line_items.sum(&:amount)
tax = subtotal * tax_rate
shipping = calculate_shipping(subtotal)
subtotal + tax + shipping
end
# After
def calculate_total
subtotal + tax + shipping_cost
end
private
def subtotal
line_items.sum(&:amount)
end
def tax
subtotal * tax_rate
end
def shipping_cost
calculate_shipping(subtotal)
end
When : Class > 100 lines or has multiple responsibilities
# Before: User class handling authentication AND profile management
class User < ApplicationRecord
def authenticate(password)
# Authentication logic
end
def update_profile(params)
# Profile logic
end
def send_welcome_email
# Email logic
end
end
# After: Separated concerns
class User < ApplicationRecord
has_one :user_profile
def authenticate(password)
# Authentication only
end
end
class UserProfile < ApplicationRecord
belongs_to :user
def update(params)
# Profile management
end
end
class UserNotifier
def self.send_welcome(user)
# Email logic
end
end
When : Logic spans multiple models or has complex orchestration
# Before: Fat controller or model method
class PolicyRenewalService
def initialize(policy, new_expiry_date)
@policy = policy
@new_expiry_date = new_expiry_date
end
def call
return failure("Not renewable") unless @policy.renewable?
ApplicationRecord.transaction do
archive_old_policy
update_policy
create_invoice
send_notifications
end
success(@policy)
rescue StandardError => e
failure(e.message)
end
end
When : Complex conditionals based on type
# Before: Type checking
def calculate_premium
case insurance_type
when 'auto'
base_rate * vehicle_factor * driver_age_factor
when 'home'
base_rate * property_value_factor * location_risk
when 'life'
base_rate * age_factor * health_factor
end
end
# After: Polymorphism
class AutoInsurancePolicy < Insurance::Policy
def calculate_premium
base_rate * vehicle_factor * driver_age_factor
end
end
class HomeInsurancePolicy < Insurance::Policy
def calculate_premium
base_rate * property_value_factor * location_risk
end
end
When : Method has > 3 parameters or parameter groups appear together
# Before
def create_policy(policy_number, effective_date, expiry_date, premium, person_id, company_id)
# ...
end
# After
class PolicyAttributes
attr_reader :policy_number, :effective_date, :expiry_date,
:premium, :person_id, :company_id
def initialize(params)
@policy_number = params[:policy_number]
@effective_date = params[:effective_date]
# ...
end
def valid?
# Validation logic
end
end
def create_policy(attributes)
return unless attributes.valid?
# ...
end
Use blocks and enumerables :
# ✅ Good
users.select(&:active?).map(&:email)
# ❌ Bad
result = []
users.each do |user|
result << user.email if user.active?
end
Use symbols for keys :
# ✅ Good
{ name: 'John', age: 30 }
# ❌ Bad
{ 'name' => 'John', 'age' => 30 }
Skinny controllers, focused models :
Use scopes for queries :
# ✅ Good
class Policy < ApplicationRecord
scope :active, -> { where(status: 'active') }
scope :expiring_soon, -> { where('expiry_date < ?', 30.days.from_now) }
end
# ❌ Bad
def self.active_policies
where(status: 'active')
end
When providing refactoring recommendations:
List identified issues with severity (High/Medium/Low) and location
Prioritized list of refactoring steps
Concrete before/after code samples
Required test changes or additions
How to safely deploy changes
Before finalizing recommendations:
| Smell | Pattern | When to Use |
|---|---|---|
| Long Method | Extract Method | Method > 10-15 lines |
| Large Class | Extract Class | Class > 100 lines |
| Long Parameter List | Parameter Object | > 3 parameters |
| Feature Envy | Move Method | Uses other object's data |
| Primitive Obsession | Extract Value Object | Primitives with behavior |
| Complex Conditional | Polymorphism | Type-based conditionals |
| Duplicated Code | Extract Method/Module | Same code in 2+ places |
Ask questions when uncertain about:
Remember: The goal is maintainable code that the team understands, not perfect code.
Weekly Installs
0
Repository
GitHub Stars
4
First Seen
Jan 1, 1970
Security Audits
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
107,800 周安装