AgentSkillsCN

clean-code

参考罗伯特·马丁的著作,掌握清洁代码的原则与实践。在审查代码质量、命名规范、重构代码,或应用SOLID原则时使用此功能。在检测代码异味、改进代码风格,以及解决可维护性问题时自动激活。

SKILL.md
--- frontmatter
name: clean-code
description: "Clean Code principles and practices reference based on Robert Martin's work. Use this skill when reviewing code quality, naming conventions, refactoring, or applying SOLID principles. Auto-activates for code smell detection, style improvements, and maintainability concerns."

Clean Code Reference

A comprehensive reference for writing clean, maintainable code based on Robert Martin's "Clean Code" and related works. This skill provides language-agnostic principles with examples that apply to any programming language.

When This Skill Activates

This skill automatically activates when you:

  • Review code for quality or maintainability
  • Need guidance on naming conventions
  • Discuss refactoring or code smells
  • Apply SOLID principles
  • Improve code readability or structure
  • Write or review unit tests

Quick Reference

SOLID Principles

PrincipleSummaryViolation Sign
Single Responsibility (SRP)One reason to changeClass does too many things
Open/Closed (OCP)Open for extension, closed for modificationModifying existing code for new features
Liskov Substitution (LSP)Subtypes must be substitutableSubclass breaks parent behavior
Interface Segregation (ISP)No forced dependencies on unused methodsFat interfaces
Dependency Inversion (DIP)Depend on abstractions, not concretionsHigh-level modules import low-level

Clean Code Practices

PracticeKey Points
Meaningful NamesIntention-revealing, pronounceable, searchable
FunctionsSmall, do one thing, one level of abstraction
CommentsCode should be self-documenting; comments explain why, not what
FormattingConsistent style, vertical density, horizontal alignment
Error HandlingExceptions over error codes, fail fast, don't return null
Unit TestingF.I.R.S.T. principles, one assert per test, test behavior
Code SmellsRecognition and refactoring of common problems
Boy Scout RuleLeave code cleaner than you found it

The Boy Scout Rule

"Leave the campground cleaner than you found it."

Every time you touch code, make it a little better. Small, incremental improvements compound over time.

Code Quality Checklist

Before committing code, verify:

  • Names - Are all names intention-revealing?
  • Functions - Is each function doing exactly one thing?
  • Comments - Are comments explaining why, not what?
  • DRY - Is there any duplicated logic?
  • SOLID - Are principles being followed?
  • Tests - Is the code tested and testable?
  • Error Handling - Are errors handled gracefully?

Language Translation Notes

Examples use generic pseudocode. Adapt to your language:

  • PHP: class, function, ->, type hints
  • JavaScript/TypeScript: class, arrow functions, .
  • Python: class, def, ., type hints
  • Java/C#: Direct mapping with access modifiers

Based on concepts from "Clean Code: A Handbook of Agile Software Craftsmanship" by Robert C. Martin, 2008.