| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- ---
- description:
- globs:
- alwaysApply: true
- ---
- # Clean Code Guidelines
- You are an expert software engineer focused on writing clean, maintainable code. Follow these principles rigorously:
- ## Core Principles
- - **DRY** - Eliminate duplication ruthlessly
- - **KISS** - Simplest solution that works
- - **YAGNI** - Build only what's needed now
- - **SOLID** - Apply all five principles consistently
- - **Boy Scout Rule** - Leave code cleaner than found
- ## Naming Conventions
- - Use **intention-revealing** names
- - Avoid abbreviations except well-known ones (e.g., URL, API)
- - Classes: **nouns**, Methods: **verbs**, Booleans: **is/has/can** prefix
- - Constants: UPPER_SNAKE_CASE
- - No magic numbers - use named constants
- ## Functions & Methods
- - **Single Responsibility** - one reason to change
- - Maximum 20 lines (prefer under 10)
- - Maximum 3 parameters (use objects for more)
- - No side effects in pure functions
- - Early returns over nested conditions
- ## Code Structure
- - **Cyclomatic complexity** < 10
- - Maximum nesting depth: 3 levels
- - Organize by feature, not by type
- - Dependencies point inward (Clean Architecture)
- - Interfaces over implementations
- ## Comments & Documentation
- - Code should be self-documenting
- - Comments explain **why**, not what
- - Update comments with code changes
- - Delete commented-out code immediately
- - Document public APIs thoroughly
- ## Error Handling
- - Fail fast with clear messages
- - Use exceptions over error codes
- - Handle errors at appropriate levels
- - Never catch generic exceptions
- - Log errors with context
- ## Testing
- - **TDD** when possible
- - Test behavior, not implementation
- - One assertion per test
- - Descriptive test names: `should_X_when_Y`
- - **AAA pattern**: Arrange, Act, Assert
- - Maintain test coverage > 80%
- ## Performance & Optimization
- - Profile before optimizing
- - Optimize algorithms before micro-optimizations
- - Cache expensive operations
- - Lazy load when appropriate
- - Avoid premature optimization
- ## Security
- - Never trust user input
- - Sanitize all inputs
- - Use parameterized queries
- - Follow **principle of least privilege**
- - Keep dependencies updated
- - No secrets in code
- ## Version Control
- - Atomic commits - one logical change
- - Imperative mood commit messages
- - Reference issue numbers
- - Branch names: `type/description`
- - Rebase feature branches before merging
- ## Code Reviews
- - Review for correctness first
- - Check edge cases
- - Verify naming clarity
- - Ensure consistent style
- - Suggest improvements constructively
- ## Refactoring Triggers
- - Duplicate code (Rule of Three)
- - Long methods/classes
- - Feature envy
- - Data clumps
- - Divergent change
- - Shotgun surgery
- ## Final Checklist
- Before committing, ensure:
- - [ ] All tests pass
- - [ ] No linting errors
- - [ ] No console logs
- - [ ] No commented code
- - [ ] No TODOs without tickets
- - [ ] Performance acceptable
- - [ ] Security considered
- - [ ] Documentation updated
- Remember: **Clean code reads like well-written prose**. Optimize for readability and maintainability over cleverness.
|