Agent skill
clean-code-practices
Use when the Developer is writing or modifying code, implementing features, fixing bugs, or refactoring. Activates for any code writing task to ensure clean, readable, maintainable code following KISS, DRY, and YAGNI principles.
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/clean-code-practices
SKILL.md
Clean Code Practices
When This Applies
Apply this guidance whenever writing or modifying source code as the Developer role.
Core Principles
KISS — Keep It Simple
- Choose the simplest approach that works
- Avoid premature optimization
- If a solution needs a comment to explain what it does, simplify it
DRY — Don't Repeat Yourself
- Extract common logic into shared functions only when used 3+ times
- Don't create abstractions for two similar lines — wait for the third
- Prefer duplication over the wrong abstraction
YAGNI — You Aren't Gonna Need It
- Only implement what the task requires
- Don't add features "just in case"
- Don't design for hypothetical future requirements
Naming
- Variables: Describe what they hold —
userEmailnote,totalPricenottp - Functions: Describe what they do —
calculateTax()notprocess(),isValid()notcheck() - Booleans: Use is/has/can prefix —
isActive,hasPermission,canDelete - Constants: UPPER_SNAKE_CASE —
MAX_RETRIES,DEFAULT_TIMEOUT - Be consistent: If the codebase uses
get*for retrievals, don't introducefetch*
Function Design
- Single responsibility — One function does one thing
- Short — If a function exceeds 30 lines, consider breaking it down
- Few parameters — 3 or fewer. If more, use an options object
- No side effects — Unless the function name clearly indicates mutation
- Early returns — Handle error cases first, then the happy path
// Prefer early returns
function process(input) {
if (!input) return null;
if (!input.isValid) throw new Error("Invalid input");
// Happy path here
return transform(input);
}
Error Handling
- Handle errors at the appropriate level (not too early, not too late)
- Never swallow errors silently
- Include context in error messages — what failed, what was expected
- Use specific error types, not generic catch-all
- Validate at system boundaries (user input, API calls), trust internal code
Code Organization
- Group related code together
- Keep imports organized (stdlib, external, internal)
- One concept per file — don't mix unrelated functionality
- Follow the project's existing patterns for file and folder structure
What NOT To Do
- Don't add comments that restate the code (
// increment counterabovecounter++) - Don't leave TODO comments without task IDs
- Don't commit debug logging, console.log, or print statements
- Don't add empty catch blocks
- Don't use magic numbers — use named constants
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?