| name | refactor-code |
| description | Refactor and simplify code |
name: refactor-code
description: Refactor and simplify code
Instructions for Refactoring and Simplifying Code
When refactoring code in the Canzeltly project, follow these guidelines to improve maintainability, readability, and performance:
General Principles
- DRY (Don't Repeat Yourself): Eliminate duplicate code by extracting common logic into shared utilities or components.
- Single Responsibility: Ensure each function, class, or module has one clear purpose.
- Readability: Use descriptive names, clear structure, and comments for complex logic.
- Consistency: Follow the project's coding conventions, such as using Zod for types, Prettier for formatting, and the established file naming patterns.
Common Refactoring Techniques
1. Extract Functions
- Break down large functions into smaller, focused functions.
- Move repeated code blocks into reusable utility functions in
src/shared/util.*.ts.
- Example: If multiple components parse route parameters, extract to a utility.
2. Simplify Conditionals
- Replace complex if-else chains with early returns or switch statements.
- Use ternary operators for simple conditions.
- Leverage Zod enums instead of string comparisons.
3. Remove Dead Code
- Delete unused imports, variables, functions, or files.
- Use tools like
npm run build to identify unused code.
4. Improve Type Safety
- Add proper TypeScript types where
any is used.
- Use Zod schemas for validation and type inference.
- Extend existing types in rather than creating new ones unnecessarily.