Claude Code से Refactoring कैसे Automate करें
Claude Code से efficiently code refactoring automate करना सीखें। Real-world projects के लिए practical prompts और concrete refactoring patterns शामिल हैं।
Claude Code से Refactor क्यों करें?
सब जानते हैं कि refactoring ज़रूरी है, लेकिन manually करना tedious होता है और अक्सर deprioritize हो जाता है। Claude Code आपके पूरे project को समझता है और multiple files में autonomously refactoring execute कर सकता है।
Pattern 1: Type Safety Improve करना
any Types Eliminate करना
> Search the project for all any types and replace them with proper types.
> After the changes, verify that npx tsc --noEmit passes.
Claude Code यह कैसे handle करता है:
anytypes वाली files search करता है- Surrounding code से appropriate types infer करता है
- ज़रूरत होने पर type definition files create करता है
- Type checker run करके verify करता है कि सब compile हो रहा है
Before और After
// Before
function processData(data: any): any {
return data.items.map((item: any) => item.name);
}
// After
interface DataPayload {
items: Array<{ name: string; id: number }>;
}
function processData(data: DataPayload): string[] {
return data.items.map((item) => item.name);
}
Pattern 2: Functions Split करना और Responsibilities Clarify करना
Oversized functions को break up करना Claude Code exceptionally well handle करता है।
> The createOrder function in src/services/orderService.ts is over 200 lines.
> Split it into smaller functions following the single responsibility principle.
> Make sure existing tests still pass.
Splitting के लिए Prompt
> Split the function using these guidelines:
> - Each function should be 30 lines or fewer
> - Separate validation, business logic, and persistence
> - Add JSDoc comments to each function
> - Split tests to match the new function boundaries
Pattern 3: Error Handling Standardize करना
पूरे project में error handling patterns unify करें।
> Standardize error handling across the project:
> - Use a custom error class (AppError)
> - Define error codes as constants
> - Always include logging in try-catch blocks
Claude Code जो generate करता है उसका example:
// src/errors/app-error.ts
export class AppError extends Error {
constructor(
public readonly code: string,
message: string,
public readonly statusCode: number = 500,
public readonly cause?: Error
) {
super(message);
this.name = 'AppError';
}
}
// Error code constants
export const ErrorCodes = {
USER_NOT_FOUND: 'USER_NOT_FOUND',
INVALID_INPUT: 'INVALID_INPUT',
DB_CONNECTION_ERROR: 'DB_CONNECTION_ERROR',
} as const;
Pattern 4: Tests Add और Improve करना
जहाँ coverage कम है वहाँ tests add करें।
# Coverage reports से test additions guide करें
npx vitest run --coverage | claude -p "Identify files with low coverage and add the missing test cases"
> Bring test coverage for everything under src/services/ to at least 80%.
> Cover not just happy paths, but also edge cases and error scenarios.
Pattern 5: API Response Formats Standardize करना
> Standardize all API endpoint responses to this format:
>
> Success: { success: true, data: T }
> Failure: { success: false, error: { code: string, message: string } }
>
> Update the frontend code to match the new format.
Safe Refactoring के लिए Tips
1. पहले Tests Verify करें
> Run npm test first and make sure all existing tests pass.
> If any tests are failing, report them before making changes.
2. Incrementally काम करें
सब कुछ एक साथ करने की बजाय module by module refactor करें।
> Start by refactoring only src/services/userService.ts.
> Verify that tests pass after the changes before moving on.
3. Diff Review करें
> Show me the git diff. If it's too large, summarize the changes by file.
4. Separate Branch Use करें
git checkout -b refactor/error-handling
claude
> Start the error handling refactoring...
Real-World Example: Legacy Code Modernize करना
पुराने codebase को incrementally modernize करने का तरीका:
> Modernize this project with the following changes:
> 1. Replace var with const/let
> 2. Convert callback functions to async/await
> 3. Convert require to import
> 4. Verify tests pass after each change
> 5. Proceed one step at a time and report results at each stage
Conclusion
Claude Code के साथ, refactoring जो पहले days लगती थी वो hours में हो सकती है। Keys हैं: specific rules provide करें, incrementally काम करें, और हमेशा tests से validate करें। एक small module से शुरू करें और खुद results देखें।