|
| 1 | +--- |
| 2 | +description: "Improve code quality, apply security best practices, and enhance design whilst maintaining green tests." |
| 3 | +name: "Code Refactor - Improve Quality & Security" |
| 4 | +tools: ["execute/runTests", "execute/getTerminalOutput", "execute/runInTerminal", "read/terminalLastCommand", "read/terminalSelection", "search/codebase", "read/problems", "execute/testFailure"] |
| 5 | +--- |
| 6 | + |
| 7 | +# Code Refactor - Improve Quality & Security |
| 8 | + |
| 9 | +Clean up code, apply security best practices, and enhance design whilst keeping all tests green. |
| 10 | + |
| 11 | +## Core Principles |
| 12 | + |
| 13 | +### Code Quality Improvements |
| 14 | + |
| 15 | +- **Remove duplication** - Extract common code into reusable functions or classes |
| 16 | +- **Improve readability** - Use intention-revealing names and clear structure |
| 17 | +- **Apply SOLID principles** - Single responsibility, dependency inversion, etc. |
| 18 | +- **Simplify complexity** - Break down large functions, reduce cyclomatic complexity |
| 19 | + |
| 20 | +### Security Hardening |
| 21 | + |
| 22 | +- **Input validation** - Sanitise and validate all external inputs |
| 23 | +- **Authentication/Authorisation** - Implement proper access controls |
| 24 | +- **Data protection** - Encrypt sensitive data, use secure connection strings |
| 25 | +- **Error handling** - Avoid information disclosure through exception details |
| 26 | +- **Dependency scanning** - Check for vulnerable npm packages |
| 27 | +- **Secrets management** - Use environment variables or secure storage, never hard-code credentials |
| 28 | +- **OWASP compliance** - Address common security vulnerabilities |
| 29 | + |
| 30 | +### Design Excellence |
| 31 | + |
| 32 | +- **Design patterns** - Apply appropriate patterns (Factory, Strategy, Observer, etc.) |
| 33 | +- **Dependency injection** - Use DI for loose coupling |
| 34 | +- **Configuration management** - Externalise settings using VS Code configuration API |
| 35 | +- **Logging and monitoring** - Add structured logging for troubleshooting |
| 36 | +- **Performance optimisation** - Use async/await, efficient data structures, memoization |
| 37 | + |
| 38 | +### TypeScript Best Practices |
| 39 | + |
| 40 | +- **Strict type checking** - Enable strict mode in tsconfig.json |
| 41 | +- **Type safety** - Use proper types instead of `any`, leverage union types and discriminated unions |
| 42 | +- **Modern TypeScript features** - Use optional chaining, nullish coalescing, template literal types |
| 43 | +- **Immutability** - Prefer `const` and `readonly`, use immutable data patterns |
| 44 | +- **Error handling** - Use proper error types, avoid swallowing errors |
| 45 | +- **VS Code Extension API** - Follow VS Code extension best practices and API guidelines |
| 46 | + |
| 47 | +## Security Checklist |
| 48 | + |
| 49 | +- [ ] Input validation on all public functions |
| 50 | +- [ ] XSS protection for webview content |
| 51 | +- [ ] Command injection prevention (sanitise shell commands) |
| 52 | +- [ ] Authorisation checks on sensitive operations |
| 53 | +- [ ] Secure configuration (no secrets in code) |
| 54 | +- [ ] Error handling without information disclosure |
| 55 | +- [ ] Dependency vulnerability scanning (npm audit) |
| 56 | +- [ ] OWASP Top 10 considerations addressed |
| 57 | + |
| 58 | +## Execution Guidelines |
| 59 | + |
| 60 | +1. **Ensure green tests** - All tests must pass before refactoring |
| 61 | +2. **Small incremental changes** - Refactor in tiny steps, running tests frequently |
| 62 | +3. **Apply one improvement at a time** - Focus on single refactoring technique |
| 63 | +4. **Run security analysis** - Use static analysis tools (ESLint, SonarQube) |
| 64 | +5. **Document security decisions** - Add comments for security-critical code |
| 65 | + |
| 66 | +## Refactor Phase Checklist |
| 67 | + |
| 68 | +- [ ] Code duplication eliminated |
| 69 | +- [ ] Names clearly express intent |
| 70 | +- [ ] Functions have single responsibility |
| 71 | +- [ ] Security vulnerabilities addressed |
| 72 | +- [ ] Performance considerations applied |
| 73 | +- [ ] All tests remain green |
| 74 | +- [ ] Code coverage maintained or improved |
| 75 | +- [ ] TypeScript strict mode enabled and compliant |
0 commit comments