mcpField
C
clean-general

clean-general

github.com/ertugrul-dmr开发208122 次安装27 周前更新
代码仓库

关于 clean-general

clean-general 是一项用于代码审查的技能,帮助检查通用整洁代码原则,尤其适用于审查 AI 生成的代码。内容侧重清晰表达、避免重复,以及检查设计和边界问题。

主要功能

  1. 01避免重复知识
  2. 02优先采用清晰明确的表达
  3. 03用多态替代条件分支
  4. 04以命名常量替代魔法数字
  5. 05保持函数职责单一
  6. 06检查边界并移除死代码

适用场景

  1. 01审查 AI 生成代码中的重复
  2. 02发现含义不清或过于复杂的代码
  3. 03检查函数是否职责单一
  4. 04排查边界问题和死代码

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/ertugrul-dmr/clean-code-skills --skill clean-general

安装后在 Claude Code 中输入/clean-general确认已加载。

详细介绍

通用整洁代码原则

关键规则

G5:DRY(不要重复自己)

每条知识都应有唯一的权威表示。

# Bad - duplication
tax_rate = 0.0825
ca_total = subtotal * 1.0825
ny_total = subtotal * 1.07

# Good - single source of truth
TAX_RATES = {"CA": 0.0825, "NY": 0.07}
def calculate_total(subtotal: float, state: str) -> float:
    return subtotal * (1 + TAX_RATES[state])

G16:不要掩盖意图

不要耍小聪明,要清晰明了。

# Bad - what does this do?
return (x & 0x0F) << 4 | (y & 0x0F)

# Good - obvious intent
return pack_coordinates(x, y)

G23:优先使用多态,而非 If/Else

# Bad - will grow forever
def calculate_pay(employee):
    if employee.type == "SALARIED":
        return employee.salary
    elif employee.type == "HOURLY":
        return employee.hours * employee.rate
    elif employee.type == "COMMISSIONED":
        return employee.base + employee.commission

# Good - open/closed principle
class SalariedEmployee:
    def calculate_pay(self): return self.salary

class HourlyEmployee:
    def calculate_pay(self): return self.hours * self.rate

class CommissionedEmployee:
    def calculate_pay(self): return self.base + self.commission

G25:用命名常量替代魔法数字

# Bad
if elapsed_time > 86400:
    ...

# Good
SECONDS_PER_DAY = 86400
if elapsed_time > SECONDS_PER_DAY:
    ...

G30:函数应该只做一件事

如果可以提取出另一个函数,那你的函数就做了不止一件事。

G36:迪米特法则(避免链式调用)

# Bad - reaching through multiple objects
output_dir = context.options.scratch_dir.absolute_path

# Good - one dot
output_dir = context.get_scratch_dir()

执行检查清单

审查 AI 生成的代码时,请确认:

  • 没有重复(G5)
  • 意图清晰,没有魔法数字(G16、G25)
  • 优先使用多态,而非条件判断(G23)
  • 函数只做一件事(G30)
  • 没有违反迪米特法则(G36)
  • 已处理边界条件(G3)
  • 已移除死代码(G9)

评论

支持 Markdown