用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/ffsshhttiikk/opencode-agents-skills --skill refactoring命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
基于 SOC 职业分类
正在显示 SKILL.md
| name | Refactoring |
| description | Systematic improvement of existing code without changing external behavior |
| category | software-development |
I provide techniques for improving the structure of existing code while preserving its functionality. Refactoring is the art of transforming code into a better version of itself—cleaner, simpler, more maintainable—without altering what the code does. I help identify code smells, apply transformation patterns, and ensure that improvements don't introduce regressions.
Refactor when adding new features, fixing bugs, or during code reviews when you notice opportunities for improvement. Apply the "Rule of Three" — code can be copied once, but the third time needs refactoring. Refactor when code is hard to understand, when tests are difficult to write, or when you see duplication. Never refactor without tests that verify behavior. Avoid refactoring production code without proper safeguards.
# Before: Long method doing multiple things
def process_order(order_data: dict) -> dict:
# Validate order
if "customer_id" not in order_data:
raise ValueError("Missing customer_id")
if "items" not in order_data or not order_data["items"]:
raise ValueError("Order must have items")
# Calculate prices
subtotal = 0
for item in order_data["items"]:
subtotal += item["price"] * item["quantity"]
tax = subtotal * 0.08
total = subtotal + tax
# Apply discounts
if "discount_code" in order_data:
discount = 0.1 if order_data["discount_code"] == "SAVE10" else 0
total *= (1 - discount)
# Save order
order_data["order_id"] = generate_order_id()
order_data["subtotal"] = subtotal
order_data["tax"] = tax
order_data["total"] = total
order_data["status"] = "processed"
send_email(order_data[], , order_data)
order_data
() -> :
validate_order(order_data)
calculate_order_prices(order_data)
apply_discounts(order_data)
finalize_order(order_data)
send_order_notification(order_data)
order_data
() -> :
order_data:
ValueError()
order_data order_data[]:
ValueError()
() -> :
subtotal = (item[] * item[] item order_data[])
order_data[] = subtotal
order_data[] = subtotal *
order_data[] = subtotal *
() -> :
order_data order_data[] == :
order_data[] *=
order_data[] =
() -> :
order_data[] = generate_order_id()
order_data[] =
() -> :
send_email(order_data[], , order_data)
() -> :
uuid
(uuid.uuid4())[:]
from abc import ABC, abstractmethod
from datetime import datetime
# Before: Long switch statement
class Employee:
def __init__(self, name: str, employee_type: str, salary: float = 0):
self.name = name
self.employee_type = employee_type
self.base_salary = salary
def calculate_pay(self, hours_worked: float) -> float:
if self.employee_type == "hourly":
return hours_worked * self.base_salary
elif self.employee_type == "salaried":
return self.base_salary / 12
elif self.employee_type == "commission":
return self.base_salary * 0.1
# After: Polymorphic approach
class Employee(ABC):
def __init__(self, name: str):
self.name = name
() -> :
():
():
().__init__(name)
.hourly_rate = hourly_rate
() -> :
hours_worked * .hourly_rate
():
():
().__init__(name)
.annual_salary = annual_salary
() -> :
.annual_salary /
():
():
().__init__(name)
.base_salary = base_salary
() -> :
.base_salary *
from dataclasses import dataclass
from typing import NamedTuple
# Before: Long parameter lists
def create_report(
title: str,
author: str,
date_created: str,
start_date: str,
end_date: str,
include_charts: bool,
include_summary: bool,
format: str,
template: str,
output_path: str
) -> None:
print(f"Creating report: {title}")
# Implementation...
# After: Grouped parameters
class ReportConfig(NamedTuple):
title: str
author: str
date_created: str
class DateRange(NamedTuple):
start_date: str
end_date: str
class ReportOptions(NamedTuple):
include_charts: bool = True
include_summary: bool = True
format: str = "pdf"
template: str = "default"
class ():
output_path:
() -> :
()
# Before: Magic numbers everywhere
class Order:
def __init__(self, items: list):
self.items = items
def calculate_discount(self, order_value: float, customer_age: int) -> float:
if order_value > 100:
discount = 0.1
elif order_value > 50:
discount = 0.05
else:
discount = 0
if customer_age > 65:
discount += 0.05
return order_value * discount
def is_expired(self, order_date: str) -> bool:
from datetime import datetime, timedelta
date = datetime.strptime(order_date, "%Y-%m-%d")
return datetime.now() - date > timedelta(30)
# After: Named constants
class Order:
FREE_SHIPPING_THRESHOLD = 100.0
STANDARD_DISCOUNT_RATE = 0.05
PREMIUM_DISCOUNT_RATE = 0.1
SENIOR_DISCOUNT_RATE = 0.05
ORDER_EXPIRY_DAYS = 30
def __init__(self, items: ):
.items = items
() -> :
order_value >= .FREE_SHIPPING_THRESHOLD:
discount_rate = .PREMIUM_DISCOUNT_RATE
order_value >= .FREE_SHIPPING_THRESHOLD / :
discount_rate = .STANDARD_DISCOUNT_RATE
:
discount_rate =
customer_age >= :
discount_rate += .SENIOR_DISCOUNT_RATE
order_value * discount_rate
() -> :
datetime datetime, timedelta
date = datetime.strptime(order_date, )
datetime.now() - date > timedelta(.ORDER_EXPIRY_DAYS)
# Before: Poorly named methods
class Account:
def __init__(self, balance: float):
self.balance = balance
def x(self) -> bool:
return self.balance > 100
def do_stuff(self, amount: float) -> None:
if self.x():
self.balance -= amount
# After: Clear names, inline when appropriate
class Account:
MINIMUM_BALANCE = 100.0
def __init__(self, balance: float):
self.balance = balance
def has_minimum_balance(self) -> bool:
return self.balance >= self.MINIMUM_BALANCE
def withdraw(self, amount: float) -> None:
if self.has_minimum_balance():
self.balance -= amount