clean-code
Clean code principles, SOLID, design patterns, readability, error handling, and universal quality tooling. Load when writing or reviewing code.
适合你,如果你在写或审查代码时希望遵循最佳实践
npx oh-my-skill add dragoscirjan/opencode-config/clean-codecurl -fsSL https://oh-my-skill.com/install.sh | bash -s -- dragoscirjan/opencode-config/clean-codenpx oh-my-skill verify dragoscirjan/opencode-config/clean-code怎么用
商店整理自技能原文 · 版本 f561928 · 表述以原文为准Claude 会按照整洁代码原则、SOLID 原则和设计模式来编写或审查代码,注重可读性、错误处理和代码质量。
当你要求编写或审查代码时,Claude 会应用这些原则。
技能原文 SKILL.md
Clean Code & Design Principles
Readability
- Code should read like well-written prose — clear intent at every level
- Use meaningful, descriptive names for variables, functions, classes, and modules
- Keep functions short and focused — each function does one thing well
- Avoid deep nesting — extract early returns, guard clauses, and helper functions
- Write meaningful comments for _why_, not _what_ — the code itself should explain the what
Clean Code Principles
- KISS — Keep It Simple. Prefer the simplest solution that works correctly.
- DRY — Don't Repeat Yourself. Extract shared logic, but don't over-abstract prematurely.
- YAGNI — You Aren't Gonna Need It. Don't add functionality "just in case."
- Boy Scout Rule — Leave the code cleaner than you found it.
- Fail Fast — Validate inputs early, surface errors immediately.
- Least Surprise — Code should behave as the reader expects.
SOLID Principles
- Single Responsibility — A class/module should have one reason to change
- Open/Closed — Open for extension, closed for modification
- Liskov Substitution — Subtypes must be substitutable for their base types
- Interface Segregation — Prefer small, focused interfaces over large ones
- Dependency Inversion — Depend on abstractions, not concretions
Design Patterns
- Apply design patterns where they solve a real problem — not for their own sake
- Favor composition over inheritance
- Use dependency injection for testability and flexibility
- Prefer immutable data structures when practical
- Use the Strategy pattern for interchangeable behaviors, Observer for event-driven logic, Factory for complex object creation — but only when the pattern genuinely simplifies the code
Error Handling
- Handle errors properly — no swallowed exceptions
- Use typed/specific errors over generic ones
- Provide meaningful error messages with context
- Clean up resources in error paths (finally, defer, using, etc.)
Universal Quality Tools
Apply these cross-language tools on every project:
| Tool | Purpose | Reference | | ------------------------- | ---------------------------------------------- | ----------------------------------- | | .editorconfig | Cross-editor indent, charset, EOL consistency | https://editorconfig.org | | jscpd | Copy-paste detection (all languages) | https://github.com/kucherenko/jscpd | | Semgrep | Multi-language static analysis, custom rules | https://semgrep.dev | | SonarQube / SonarLint | Code quality + security dashboard | https://www.sonarsource.com | | MegaLinter | Meta-linter for CI (orchestrates 100+ linters) | https://megalinter.io | | pre-commit | Git hook framework for linters/formatters | https://pre-commit.com | | Trunk | Auto-detect and run linters/formatters | https://trunk.io | | Taskfile | Task runner for build/test/lint/format | https://taskfile.dev | | commitlint | Commit message convention enforcement | https://commitlint.js.org |
- Every project must have an
.editorconfigat the root - Use
run-quality-checksto enforce duplicated code checks to catch duplicated code blocks - Use
run-quality-checksto enforce linting/formatting before commit - Use
Taskfilefor consistenttask lint,task format,task test,task validatecommands