Clean Code
Purpose
Improve code readability, maintainability, testability, and consistency without introducing unnecessary complexity.
Core Principles
- Use meaningful and consistent names.
- Keep functions focused on a single responsibility.
- Prefer simple solutions over unnecessary abstractions.
- Avoid duplicated business logic.
- Follow SOLID principles when applicable.
- Prefer explicit dependencies over hidden coupling.
- Handle errors clearly and consistently.
- Keep code easy to test and maintain.
Workflow
When writing or refactoring code:
- Understand the existing behavior and requirements.
- Identify concrete code quality problems.
- Propose the smallest reasonable improvement.
- Preserve existing functionality unless changes are requested.
- Follow the project's established architecture and conventions.
- Add or update relevant tests when appropriate.
- Explain important changes and trade-offs.
Refactoring Guidelines
- Do not refactor unrelated modules.
- Do not introduce design patterns without a clear benefit.
- Avoid premature optimization.
- Prefer composition when inheritance adds unnecessary coupling.
- Keep public APIs compatible unless breaking changes are explicitly approved.
- Do not silently change business rules.
- Avoid adding dependencies without justification.
Validation
Before completing a task:
- Check that the requested behavior is implemented.
- Review naming, readability, and duplication.
- Check error handling and edge cases.
- Run relevant tests and static analysis when available.
- Report any tests that could not be executed.
Output Expectations
When suggesting improvements:
- Explain the identified problem.
- Describe the proposed change.
- Implement the change when authorized.
- Explain relevant trade-offs.
- Summarize validation results.
Project Adaptation
Follow repository-specific instructions and technology conventions.
Do not assume the project uses a particular language, framework, database, or architecture unless confirmed by the repository context.