Code Quality & Clean Code
Use this skill for implementation, refactoring, and review when you need practical guidance rather than a textbook.
Priorities
Apply these in order:
- Correctness
- Simplicity
- Consistency with the repo
- Operability (errors, logs, debuggability)
- Performance where it matters
Do not trade correctness for cleverness.
Core Rules
1. Prefer the smallest safe change
- Fix the root cause, not just the symptom.
- Change only the files and behaviors needed for the task.
- Avoid opportunistic refactors unless they directly reduce risk for the task at hand.
2. Reuse existing patterns
- Match the repository's naming, file layout, error-handling style, and testing approach.
- Prefer extending an existing abstraction over inventing a new one.
- If the repo already has two patterns for the same concern, follow the more local one unless there is a strong reason not to.
3. Keep control flow obvious
- Prefer flat, readable code over indirection.
- Avoid deep nesting when early returns or small helpers make the path clearer.
- Make state transitions explicit.
- Do not hide business-critical work inside callbacks, decorators, magic hooks, or convenience wrappers unless the repo already standardizes on them.
4. One unit, one reason to change
- Functions should do one coherent job.
- Modules should have clear ownership boundaries.
- If one function validates input, writes data, sends notifications, and updates analytics, split the responsibilities unless the repo strongly prefers orchestration in one place.
5. Make invalid states hard to represent
- Encode invariants in types, schemas, guards, or data structures when practical.
- Prefer explicit state models over loosely coupled booleans or nullable piles.
- Validate external input at boundaries.
6. Be explicit about failures
- Never silently swallow errors.
- Add context when rethrowing or converting errors.
- Use structured logs at meaningful boundaries.
- Return or throw domain-appropriate errors; avoid generic
"something went wrong" style messages inside the code path.
7. Keep comments rare and valuable
- Prefer self-explanatory code first.
- Comment only for invariants, non-obvious tradeoffs, protocol details, or external constraints.
- Remove stale TODO-style comments instead of adding new vague ones.
8. Keep configuration boring
- Prefer explicit config over hidden defaults.
- Centralize cross-cutting configuration when the repo already has a convention for it.
- Name feature flags, environment variables, and constants clearly.
Refactoring Rules
Refactor only when at least one is true:
- it removes duplication that would otherwise make the current task risky
- it clarifies ownership or control flow required for the current task
- it reduces the chance of regression in a touched code path
Do not refactor just because the code can be made "cleaner."
Review Heuristics
When reviewing or self-checking code, look for:
Correctness
- missing edge-case handling
- partial success / partial failure paths
- race conditions or ordering assumptions
- broken invariants after updates
- unsafe defaults or silent fallbacks
Maintainability
- duplicated logic in nearby files
- helpers that hide too much
- misleading names
- functions that mix domain logic and transport/UI concerns
- abstractions that save little but cost a lot
Safety
- unvalidated external input
- privilege or authorization gaps
- unsafe retries / repeated side effects
- logging of secrets or sensitive data
- lack of timeout / cancellation / cleanup at important boundaries
Testing
- missing tests for changed behavior
- tests coupled to implementation details instead of behavior
- missing regression coverage for the exact bug or risk being addressed
Good Defaults by Task Type
New feature
- add the smallest extension point that fits the existing architecture
- keep the happy path clear
- add validation and test coverage around new behavior
Bug fix
- reproduce first when possible
- patch the narrowest root cause
- add or update a regression test if the repo has tests
Refactor
- preserve behavior
- keep changes reviewable
- validate key paths after each structural step
Review
- prioritize correctness, regressions, and security over style
- do not request abstraction for abstraction's sake
Anti-Patterns
Avoid these unless the repo explicitly depends on them:
- speculative abstractions
- giant utility modules
- boolean flag state machines with unclear ownership
- catching broad errors and returning defaults silently
- "smart" helper layers that obscure I/O or state changes
- comments that restate the code
- large rewrites when a surgical fix is enough
Quick Checklist
1---2name: code-quality3description: Concise code-quality rules for implementation and review across the stack.4license: See repository LICENSE5---67# Code Quality & Clean Code89Use this skill for implementation, refactoring, and review when you need practical guidance rather than a textbook.1011## Priorities1213Apply these in order:14151. **Correctness**162. **Simplicity**173. **Consistency with the repo**184. **Operability** (errors, logs, debuggability)195. **Performance where it matters**2021Do not trade correctness for cleverness.2223## Core Rules2425### 1. Prefer the smallest safe change2627- Fix the root cause, not just the symptom.28- Change only the files and behaviors needed for the task.29- Avoid opportunistic refactors unless they directly reduce risk for the task at hand.3031### 2. Reuse existing patterns3233- Match the repository's naming, file layout, error-handling style, and testing approach.34- Prefer extending an existing abstraction over inventing a new one.35- If the repo already has two patterns for the same concern, follow the more local one unless there is a strong reason not to.3637### 3. Keep control flow obvious3839- Prefer flat, readable code over indirection.40- Avoid deep nesting when early returns or small helpers make the path clearer.41- Make state transitions explicit.42- Do not hide business-critical work inside callbacks, decorators, magic hooks, or convenience wrappers unless the repo already standardizes on them.4344### 4. One unit, one reason to change4546- Functions should do one coherent job.47- Modules should have clear ownership boundaries.48- If one function validates input, writes data, sends notifications, and updates analytics, split the responsibilities unless the repo strongly prefers orchestration in one place.4950### 5. Make invalid states hard to represent5152- Encode invariants in types, schemas, guards, or data structures when practical.53- Prefer explicit state models over loosely coupled booleans or nullable piles.54- Validate external input at boundaries.5556### 6. Be explicit about failures5758- Never silently swallow errors.59- Add context when rethrowing or converting errors.60- Use structured logs at meaningful boundaries.61- Return or throw domain-appropriate errors; avoid generic `"something went wrong"` style messages inside the code path.6263### 7. Keep comments rare and valuable6465- Prefer self-explanatory code first.66- Comment only for invariants, non-obvious tradeoffs, protocol details, or external constraints.67- Remove stale TODO-style comments instead of adding new vague ones.6869### 8. Keep configuration boring7071- Prefer explicit config over hidden defaults.72- Centralize cross-cutting configuration when the repo already has a convention for it.73- Name feature flags, environment variables, and constants clearly.7475## Refactoring Rules7677Refactor only when at least one is true:78791. it removes duplication that would otherwise make the current task risky802. it clarifies ownership or control flow required for the current task813. it reduces the chance of regression in a touched code path8283Do not refactor just because the code can be made "cleaner."8485## Review Heuristics8687When reviewing or self-checking code, look for:8889### Correctness9091- missing edge-case handling92- partial success / partial failure paths93- race conditions or ordering assumptions94- broken invariants after updates95- unsafe defaults or silent fallbacks9697### Maintainability9899- duplicated logic in nearby files100- helpers that hide too much101- misleading names102- functions that mix domain logic and transport/UI concerns103- abstractions that save little but cost a lot104105### Safety106107- unvalidated external input108- privilege or authorization gaps109- unsafe retries / repeated side effects110- logging of secrets or sensitive data111- lack of timeout / cancellation / cleanup at important boundaries112113### Testing114115- missing tests for changed behavior116- tests coupled to implementation details instead of behavior117- missing regression coverage for the exact bug or risk being addressed118119## Good Defaults by Task Type120121### New feature122123- add the smallest extension point that fits the existing architecture124- keep the happy path clear125- add validation and test coverage around new behavior126127### Bug fix128129- reproduce first when possible130- patch the narrowest root cause131- add or update a regression test if the repo has tests132133### Refactor134135- preserve behavior136- keep changes reviewable137- validate key paths after each structural step138139### Review140141- prioritize correctness, regressions, and security over style142- do not request abstraction for abstraction's sake143144## Anti-Patterns145146Avoid these unless the repo explicitly depends on them:147148- speculative abstractions149- giant utility modules150- boolean flag state machines with unclear ownership151- catching broad errors and returning defaults silently152- "smart" helper layers that obscure I/O or state changes153- comments that restate the code154- large rewrites when a surgical fix is enough155156## Quick Checklist157158- [ ] The change follows local repo patterns159- [ ] Ownership and control flow are easy to follow160- [ ] External input is validated at the boundary161- [ ] Errors are explicit and contextual162- [ ] The change is no larger than necessary163- [ ] Tests or verification steps match the real risk164- [ ] No unnecessary abstraction was introduced