C

Skill 详情

clean-code

This skill embodies the principles of "Clean Code" by Robert C. Martin (Uncle Bob). Use it to transform "code that works" into "code that is clean."

来源平台:GitHub
来源标识:sickn33/antigravity-awesome-skills
源文件:原始说明
编程开发 超热门 GitHub 低 风险 下载 1.73万Stars 3.68万 GitHub Copilot
来源平台GitHub
文档版本SKILL.md
热度超热门
排名信号下载 1.73万
概述 安装 文档 下载

快速判断

This skill embodies the principles of "Clean Code" by Robert C. Martin (Uncle Bob). Use it to transform "code that works" into "code that is clean."

最后校验2026-05-27
来源平台GitHub
安全提示
下载副本ZIP 可用

适合任务

  • 把重复任务整理成可复用的 AI 操作流程。
  • 让 AI 在特定场景下按统一规范执行。
  • 为团队或个人工作流提供可复制的任务说明。

输入与输出

输入:任务目标、上下文材料、文件路径、约束条件或需要处理的内容。

输出:按 Skill 说明生成的文档、代码、检查结果、计划、建议或操作步骤。

示例任务

  • 使用 clean-code 帮我处理当前任务,并说明执行前需要确认的输入。
  • 根据 clean-code 的说明,给我一个安全的使用步骤清单。

安装方式

  1. 下载本站提供的 Skill ZIP 并解压。
  2. 把解压后的 Skill 目录放入当前 AI 工具支持的 skills 目录。
  3. 如需在线查看原始内容,可打开 GitHub 的 SKILL.md

在线原始地址:clean-code/SKILL.md

风险边界

使用前请检查权限、外部依赖和要处理的数据类型。不要把密码、密钥、身份信息或敏感客户资料交给未经确认的 Skill。

SKILL.md 文档介绍

Clean Code Skill

This skill embodies the principles of "Clean Code" by Robert C. Martin (Uncle Bob). Use it to transform "code that works" into "code that is clean."

🧠 Core Philosophy

> "Code is clean if it can be read, and enhanced by a developer other than its original author." — Grady Booch

When to Use

Use this skill when:

  • Writing new code: To ensure high quality from the start.
  • Reviewing Pull Requests: To provide constructive, principle-based feedback.
  • Refactoring legacy code: To identify and remove code smells.
  • Improving team standards: To align on industry-standard best practices.

1. Meaningful Names

  • Use Intention-Revealing Names: elapsedTimeInDays instead of d.
  • Avoid Disinformation: Don't use accountList if it's actually a Map.
  • Make Meaningful Distinctions: Avoid ProductData vs ProductInfo.
  • Use Pronounceable/Searchable Names: Avoid genymdhms.
  • Class Names: Use nouns (Customer, WikiPage). Avoid Manager, Data.
  • Method Names: Use verbs (postPayment, deletePage).

2. Functions

  • Small!: Functions should be shorter than you think.
  • Do One Thing: A function should do only one thing, and do it well.
  • One Level of Abstraction: Don't mix high-level business logic with low-level details (like regex).
  • Descriptive Names: isPasswordValid is better than check.
  • Arguments: 0 is ideal, 1-2 is okay, 3+ requires a very strong justification.
  • No Side Effects: Functions shouldn't secretly change global state.

3. Comments

  • Don't Comment Bad Code—Rewrite It: Most comments are a sign of failure to express ourselves in code.
  • Explain Yourself in Code:
  # Check if employee is eligible for full benefits
  if employee.flags & HOURLY and employee.age > 65:

vs

  if employee.isEligibleForFullBenefits():
  • Good Comments: Legal, Informative (regex intent), Clarification (external libraries), TODOs.
  • Bad Comments: Mumbling, Redundant, Misleading, Mandated, Noise, Position Markers.

4. Formatting

  • The Newspaper Metaphor: High-level concepts at the top, details at the bottom.
  • Vertical Density: Related lines should be close to each other.
  • Distance: Variables should be declared near their usage.
  • Indentation: Essential for structural readability.

5. Objects and Data Structures

  • Data Abstraction: Hide the implementation behind interfaces.
  • The Law of Demeter: A module should not know about the innards of the objects it manipulates. Avoid a.getB().getC().doSomething().
  • Data Transfer Objects (DTO): Classes with public variables and no functions.

6. Error Handling

  • Use Exceptions instead of Return Codes: Keeps logic clean.
  • Write Try-Catch-Finally First: Defines the scope of the operation.
  • Don't Return Null: It forces the caller to check for null every time.
  • Don't Pass Null: Leads to NullPointerException.

7. Unit Tests

  • The Three Laws of TDD:

1. Don't write production code until you have a failing unit test.

2. Don't write more of a unit test than is sufficient to fail.

3. Don't write more production code than is sufficient to pass the failing test.

  • F.I.R.S.T. Principles: Fast, Independent, Repeatable, Self-Validating, Timely.

8. Classes

  • Small!: Classes should have a single responsibility (SRP).
  • The Stepdown Rule: We want the code to read like a top-down narrative.

9. Smells and Heuristics

  • Rigidity: Hard to change.
  • Fragility: Breaks in many places.
  • Immobility: Hard to reuse.
  • Viscosity: Hard to do the right thing.
  • Needless Complexity/Repetition.

🛠️ Implementation Checklist

  • [ ] Is this function smaller than 20 lines?
  • [ ] Does this function do exactly one thing?
  • [ ] Are all names searchable and intention-revealing?
  • [ ] Have I avoided comments by making the code clearer?
  • [ ] Am I passing too many arguments?
  • [ ] Is there a failing test for this change?

Limitations

  • Use this skill only when the task clearly matches the scope described above.
  • Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
  • Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.
建议反馈