mcpField
C
clean-functions

clean-functions

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

关于 clean-functions

clean-functions 是一项聚焦函数简洁性和可预测性的代码质量技能。它提供减少参数、避免副作用和标志参数,以及移除未使用函数的规则。

主要功能

  1. 01函数参数不超过三个
  2. 02返回值而非修改输入
  3. 03避免布尔标志参数
  4. 04删除未使用的函数

适用场景

  1. 01审查参数过多的函数
  2. 02发现修改参数的副作用
  3. 03拆分布尔标志隐藏的不同逻辑
  4. 04清理未调用的函数

安装

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

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

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

详细介绍

简洁函数

F1:参数过多(最多 3 个)

# Bad - too many parameters
def create_user(name, email, age, country, timezone, language, newsletter):
    ...

# Good - use a dataclass or dict
@dataclass
class UserData:
    name: str
    email: str
    age: int
    country: str
    timezone: str
    language: str
    newsletter: bool

def create_user(data: UserData):
    ...

参数超过 3 个,说明函数做的事情太多,或者需要使用数据结构。

F2:不使用输出参数

不要通过副作用修改参数,而应返回值。

# Bad - modifies argument
def append_footer(report: Report) -> None:
    report.append("\n---\nGenerated by System")

# Good - returns new value
def with_footer(report: Report) -> Report:
    return report + "\n---\nGenerated by System"

F3:不使用标志参数

布尔标志意味着函数至少做了两件事。

# Bad - function does two different things
def render(is_test: bool):
    if is_test:
        render_test_page()
    else:
        render_production_page()

# Good - split into two functions
def render_test_page(): ...
def render_production_page(): ...

F4:删除无用函数

如果函数没有被调用,就删除它。不要保留“以防万一”的代码。Git 会保存历史记录。

评论

支持 Markdown