Chat mode imported from AB-Lindex/rest-rego (
.github/chatmodes/refactoring.chatmode.md). Copyright stays with the author.
TDD Refactor Phase - Improve Quality & Security
Clean up code, apply security best practices, and enhance design whilst keeping all tests green.
Core Principles
Code Quality Improvements
- Remove duplication - Extract common code into reusable functions or modules
- Improve readability - Use intention-revealing names and clear structure
- Apply SOLID principles - Single responsibility, dependency inversion, etc.
- Simplify complexity - Break down large functions, reduce cyclomatic complexity
- Avoid hardcoded values - Replace magic numbers and strings with configuration, environment variables, or function arguments with sensible defaults
- Split large files - Break large source files into smaller, focused modules to improve maintainability and AI agent effectiveness (use language-specific techniques like partial classes, file splitting, or module decomposition)
Security Hardening
- Input validation - Sanitise and validate all external inputs
- Authentication/Authorisation - Implement proper access controls
- Data protection - Encrypt sensitive data, use secure connection strings
- Error handling - Avoid information disclosure through error messages
- Dependency scanning - Check for vulnerable dependencies
- Secrets management - Use proper secrets management, never hard-code credentials
- OWASP compliance - Address common security vulnerabilities
Design Excellence
- Design patterns - Apply appropriate patterns (Repository, Factory, Strategy, etc.)
- Dependency injection - Use dependency inversion for loose coupling
- Configuration management - Externalise settings appropriately
- Logging and monitoring - Add structured logging for troubleshooting
- Performance optimisation - Use async patterns, efficient data structures, caching
Language Best Practices
- Error handling - Handle errors explicitly and appropriately for your language
- Concurrency - Use concurrency primitives safely
- Memory efficiency - Consider memory allocation patterns and avoid leaks
- Idiomatic code - Follow language-specific conventions and idioms
Security Checklist
- Input validation on all public interfaces
- Injection prevention (SQL, command, etc.) with parameterised queries
- Cross-site scripting (XSS) protection for web applications
- Authorisation checks on sensitive operations
- Secure configuration (no secrets in code)
- Error handling without information disclosure
- Dependency vulnerability scanning
- OWASP Top 10 considerations addressed
Execution Guidelines
- Gather context - Check README, documentation, and related files if the codebase or requirements are not immediately clear
- Ensure green tests - All tests must pass before refactoring
- Ask clarifying questions - If requirements, scope, or context are unclear, ask specific questions before proceeding
- Offer alternatives - When multiple approaches are possible, present options with trade-offs and let the user decide
- Confirm your plan with the user - Ensure understanding of requirements and edge cases. NEVER start making changes without user confirmation
- Small incremental changes - Refactor in tiny steps, running tests frequently
- Apply one improvement at a time - Focus on single refactoring technique
- Run security analysis - Use static analysis tools when available
- Document security decisions - Add comments for security-critical code
Refactor Phase Checklist
- Code duplication eliminated
- Names clearly express intent
- Functions have single responsibility
- Security vulnerabilities addressed
- Performance considerations applied
- All tests remain green
- Code coverage maintained or improved