agentleFS
28672032962867203296/hermes-agent/skills/software-development/refactoring-patterns/SKILL.md

refactoring-patterns

Skill0 starsChanged 3 months agoMITView on GitHub

重构手法:提取函数、内联、移动、简化条件、拆分循环.

9 other projects use a version of this.

Adopted by

Most recently taken up by

The file

Files
SKILL.md

重构手法

代码坏味道(看到就要考虑重构)

1. 函数太长(> 30 行)
2. 参数太多(> 4 个)
3. 重复代码
4. 深层嵌套(> 3 层)
5. 神秘命名(看不懂的名字)
6. 全局可变状态
7. 死代码(注释掉/从未调用)
8. 大类(> 200 行)
9. 霰弹式修改(改一个功能要动很多文件)
10. 依恋情结(函数用别的类的数据多过自己的)

常用重构手法

提取函数

# 重构前
def process_order(order):
    # 计算总价(10行)
    total = 0
    for item in order.items:
        if item.discount:
            total += item.price * 0.8
        else:
            total += item.price
    total += calculate_shipping(order)
    # 验证库存(8行)
    for item in order.items:
        if item.stock < 1:
            raise OutOfStock(item)
    # 保存订单(5行)
    db.save(order)

# 重构后
def process_order(order):
    total = calculate_total(order)
    validate_stock(order)
    save_order(order, total)

def calculate_total(order): ...
def validate_stock(order): ...
def save_order(order, total): ...

简化条件

# 重构前
if date.before(SUMMER_START) or date.after(SUMMER_END):
    charge = quantity * winterRate + winterServiceCharge
else:
    charge = quantity * summerRate

# 重构后
if is_summer(date):
    charge = summer_charge(quantity)
else:
    charge = winter_charge(quantity)

卫语句(早返回)

# 重构前
def get_pay_amount():
    if is_dead:
        result = dead_amount()
    else:
        if is_separated:
            result = separated_amount()
        else:
            if is_retired:
                result = retired_amount()
            else:
                result = normal_amount()
    return result

# 重构后
def get_pay_amount():
    if is_dead: return dead_amount()
    if is_separated: return separated_amount()
    if is_retired: return retired_amount()
    return normal_amount()

以多态取代条件

# 重构前
def get_speed(bird):
    if bird.type == "EUROPEAN":
        return base_speed
    elif bird.type == "AFRICAN":
        return base_speed - load_factor * bird.coconuts
    elif bird.type == "NORWEGIAN":
        return 0 if bird.is_nailed else base_speed * voltage

# 重构后
class European: speed = base_speed
class African: speed = base_speed - load_factor * coconuts
class Norwegian: speed = 0 if nailed else base_speed * voltage

重构安全网

1. 重构前确保有测试
2. 一次只改一处,改完跑测试
3. 小步提交(每步可回退)
4. 不要同时重构和加新功能

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.