帮助你识别职责混杂的例程,并拆分为单一职责的清晰结构。
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "Keeping Routines Focused" 技能: 1. 下载 https://raw.githubusercontent.com/obra/clank/main/skills/coding/keeping-routines-focused/SKILL.md 2. 保存为 ~/.claude/skills/keeping-routines-focused/SKILL.md 3. 装好后重载技能,告诉我可以用了
请检查下面这段代码,找出一个函数中混合了哪些不同职责,并给出按单一职责原则拆分后的重构方案与示例代码。
指出函数承担的多项任务,并提供拆分后的函数设计与重构代码示例。
请从可维护性角度评审这些例程,判断哪些例程做了不止一件事,并说明应如何提取成更专注的小例程。
给出问题例程清单、职责划分说明,以及建议的拆分方式。
基于“每个例程只做一件事”的原则,分析这段模块代码,并输出可执行的重构清单,按优先级排序。
输出按优先级排列的重构建议,帮助逐步提高代码聚焦度与可读性。
A routine should do ONE thing and do it well. This is called functional cohesion - the strongest, best kind of cohesion.
Core principle: If a routine's description contains "and", it's doing too many things. Extract into focused routines.
Goal: Improve intellectual manageability. The more focused a routine, the easier to understand, test, modify, and reuse.
Proactively (writing new code):
Reactively (improving existing code):
Warning signs routine needs focus:
One thing means one level of abstraction:
✅ Does one thing:
def calculate_total_price(items, tax_rate):
"""Calculate total price of items including tax."""
subtotal = sum(item.price * item.quantity for item in items)
tax = subtotal * tax_rate
return subtotal + tax
Single purpose: price calculation. All statements at same abstraction level (arithmetic).
❌ Does multiple things:
def handle_order(order_data, user_id):
"""
Process order:
- Validate order data
- Calculate total with tax
- Apply discount codes
- Check inventory
- Create order record
- Send confirmation email
- Update user history
- Return confirmation
"""
validated = validate_order_data(order_data, user_id) # Thing 1
subtotal = calculate_subtotal(validated["items"]) # Thing 2
discount = apply_discount(subtotal, validated.get("discount_code")) # Thing 3
tax = calculate_tax(subtotal - discount, validated["tax_rate"]) # Thing 4
total = subtotal - discount + tax # Thing 5
check_inventory(validated["items"]) # Thing 6
order_record = create_order_record(...) # Thing 7
send_confirmation_email(...) # Thing 8
update_user_history(user_id, order_record["order_id"]) # Thing 9
return {...} # Thing 10
Description has 8 "and"s. Does 10 different things. Violates single responsibility.
Group related statements, extract into focused routine:
Before (orchestrator does everything):
def handle_order(order_data, user_id):
# Validation (lines 1-10)
validated = validate_order_data(order_data, user_id)
# Pricing (lines 11-15)
subtotal = calculate_subtotal(validated["items"])
discount = apply_discount(subtotal, validated.get("discount_code"))
tax = calculate_tax(subtotal - discount, validated["tax_rate"])
total = subtotal - discount + tax
# Inventory (lines 16-20)
check_inventory(validated["items"])
# Persistence (lines 21-30)
order_record = create_order_record(...)
# Notifications (lines 31-35)
send_confirmation_email(...)
update_user_history(user_id, order_record["order_id"])
return {...}
After (each phase is focused routine):
def handle_order(order_data, user_id):
"""Single responsibility: Orchestrate order processing."""
validated_order = validate_order_request(order_data, user_id)
pricing = calculate_order_pricing(validated_order)
verify_inventory_available(validated_order)
order = create_and_save_order(validated_order, pricing, user_id)
…
先用伪代码梳理方案与迭代思路,再高效转成可执行代码。
帮助开发者用早返回或表驱动方式简化嵌套条件分支,提升代码可读性。
帮助你为变量选择清晰准确、易维护的命名,提升代码可读性。
帮助开发者保持类接口抽象一致,避免混杂序列化、持久化等无关职责。
帮助你撰写不过时的代码注释,聚焦做什么与为什么而非时序背景。
帮助用户检索过往 Claude Code 对话,快速找回事实、决策与上下文线索。
帮助开发者用单一职责变量提升代码可读性、可维护性与调试效率
扫描多项技能并提炼通用规则,自动追加、修订或新建规则文件。
帮助用户用接口封装实现细节,从业务层面设计与理解系统
扫描现有技能并提炼通用原则,生成或更新可复用规则文件。
沉淀并查询代码库经验教训,帮助开发、验证与评审减少重复踩坑。
帮助你调研、规划并并行执行大规模代码变更,让多个代理分别提交 PR。