Clean Code

作者 pproencacf93c57cac89無授權條款215 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫7 週前更新

Use when writing, reviewing, or refactoring code for maintainability and readability. Triggers on code reviews, naming discussions, function design, error handling, and test writing. Based on Robert C. Martin's Clean Code handbook with modern corrections.

AI 產生的概覽

一份包含 48 條 Clean Code 規則的參考指南,用於撰寫、審查與重構可維護的程式碼。

功能
此技能提供源自 Robert C. Martin《Clean Code》的 48 條軟體工藝規則,依優先順序分為 10 個類別,涵蓋命名、函式、註解、格式、錯誤處理、物件、邊界、類別、單元測試與簡單設計。每條規則都有獨立的參考檔案,內含說明與程式碼範例,範例以 Java 為主,但宣稱與語言無關。它產出的是用於程式碼審查、重構決策與新開發的指引,而非產生檔案或執行工具。
適用情境
在撰寫新的函式、類別或模組,命名變數與方法,或審查程式碼可維護性時使用。它也適合為了提升清晰度而重構現有程式碼、改善單元測試,以及封裝第三方相依套件。
執行需求
不需要指令碼、套件或憑證;這是僅含說明與參考檔案的技能,只需代理讀取其 Markdown 檔案。

Robert C. Martin (Uncle Bob) Clean Code Best Practices

Comprehensive software craftsmanship guide based on Robert C. Martin's "Clean Code: A Handbook of Agile Software Craftsmanship", updated with modern corrections where the original 2008 advice has been superseded. Contains 48 rules across 10 categories, prioritized by impact to guide code reviews, refactoring decisions, and new development. Examples are primarily in Java but principles are language-agnostic.

When to Apply

Reference these guidelines when:

  • Writing new functions, classes, or modules
  • Naming variables, functions, classes, or files
  • Reviewing code for maintainability issues
  • Refactoring existing code to improve clarity
  • Writing or improving unit tests
  • Wrapping third-party dependencies

Rule Categories by Priority

PriorityCategoryImpactPrefix
1Meaningful NamesCRITICALname-
2FunctionsCRITICALfunc-
3CommentsHIGHcmt-
4FormattingHIGHfmt-
5Error HandlingHIGHerr-
6Objects and Data StructuresMEDIUM-HIGHobj-
7BoundariesMEDIUM-HIGHbound-
8Classes and SystemsMEDIUM-HIGHclass-
9Unit TestsMEDIUMtest-
10Emergence and Simple DesignMEDIUMemerge-

Quick Reference

1. Meaningful Names (CRITICAL)

  • name-intention-revealing [blocked] - Use names that reveal intent
  • name-avoid-disinformation [blocked] - Avoid misleading names
  • name-meaningful-distinctions [blocked] - Make meaningful distinctions
  • name-pronounceable [blocked] - Use pronounceable names
  • name-searchable [blocked] - Use searchable names
  • name-avoid-encodings [blocked] - Avoid encodings in names
  • name-class-noun [blocked] - Use noun phrases for class names
  • name-method-verb [blocked] - Use verb phrases for method names

2. Functions (CRITICAL)

  • func-small [blocked] - Keep functions small
  • func-one-thing [blocked] - Functions should do one thing
  • func-abstraction-level [blocked] - Maintain one level of abstraction
  • func-minimize-arguments [blocked] - Minimize function arguments
  • func-no-side-effects [blocked] - Avoid side effects
  • func-command-query-separation [blocked] - Separate commands from queries
  • func-dry [blocked] - Do not repeat yourself

3. Comments (HIGH)

  • cmt-express-in-code [blocked] - Express yourself in code, not comments
  • cmt-explain-intent [blocked] - Use comments to explain intent
  • cmt-avoid-redundant [blocked] - Avoid redundant comments
  • cmt-avoid-commented-out-code [blocked] - Delete commented-out code
  • cmt-warning-consequences [blocked] - Use warning comments for consequences

4. Formatting (HIGH)

  • fmt-vertical-formatting [blocked] - Use vertical formatting for readability
  • fmt-horizontal-alignment [blocked] - Avoid horizontal alignment
  • fmt-team-rules [blocked] - Follow team formatting rules
  • fmt-indentation [blocked] - Respect indentation rules

5. Error Handling (HIGH)

  • err-use-exceptions [blocked] - Separate error handling from happy path
  • err-write-try-catch-first [blocked] - Write try-catch-finally first
  • err-provide-context [blocked] - Provide context with exceptions
  • err-define-by-caller-needs [blocked] - Define exceptions by caller needs
  • err-avoid-null [blocked] - Avoid returning and passing null

6. Objects and Data Structures (MEDIUM-HIGH)

  • obj-data-abstraction [blocked] - Hide data behind abstractions
  • obj-data-object-asymmetry [blocked] - Understand data/object anti-symmetry
  • obj-law-of-demeter [blocked] - Follow the Law of Demeter
  • obj-avoid-hybrids [blocked] - Avoid hybrid data-object structures
  • obj-dto [blocked] - Use DTOs for data transfer

7. Boundaries (MEDIUM-HIGH)

  • bound-wrap-third-party [blocked] - Wrap third-party APIs
  • bound-learning-tests [blocked] - Write learning tests for third-party code

8. Classes and Systems (MEDIUM-HIGH)

  • class-small [blocked] - Keep classes small
  • class-cohesion [blocked] - Maintain class cohesion
  • class-organize-for-change [blocked] - Organize classes for change
  • class-isolate-from-change [blocked] - Isolate classes from change
  • class-separate-concerns [blocked] - Separate construction from use

9. Unit Tests (MEDIUM)

  • test-first-law [blocked] - Follow the three laws of TDD
  • test-keep-clean [blocked] - Keep tests clean
  • test-one-assert [blocked] - One concept per test
  • test-first-principles [blocked] - Follow FIRST principles
  • test-build-operate-check [blocked] - Use Build-Operate-Check pattern

10. Emergence and Simple Design (MEDIUM)

  • emerge-simple-design [blocked] - Follow the four rules of simple design
  • emerge-expressiveness [blocked] - Maximize expressiveness

How to Use

Read individual reference files for detailed explanations and code examples:

  • Section definitions [blocked] - Category structure and impact levels
  • Rule template [blocked] - Template for adding new rules

Reference Files

FileDescription
references/_sections.md [blocked]Category definitions and ordering
assets/templates/_template.md [blocked]Template for new rules
metadata.json [blocked]Version and reference information

來源與署名

來源:pproenca/dot-skills位於skills/.experimental/clean-code提交cf93c57

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架