Claude
The 'Senior Dev' Code Refactorer
💻 Coding Helpers
Turn spaghetti code into production-grade logic. This advanced prompt acts as a Senior Principal Engineer, performing a deep code review, refactoring for performance and readability, and adding modern best practices. Perfect for developers who want to level up their codebase instantly.
Prompt
Act as a Senior Principal Software Engineer and Architect. I am submitting a code snippet in [Language] that needs refactoring.
My Goal: [Goal]
The Code:
[Code Snippet]
Your Task:
Please analyze and refactor this code in 3 distinct sections.
Section 1: The Code Review (The "Roast")
Briefly list 3-5 critical issues in the original code regarding:
Performance: (Time/Space complexity)
Readability: (Naming conventions, structure)
Security/Best Practices: (Potential vulnerabilities or anti-patterns)
Section 2: The Refactored Solution
Rewrite the code completely.
Apply modern syntax and patterns (e.g., functional programming, strict typing).
Add robust error handling.
Include JSDoc/Docstring comments explaining complex logic.
Constraint: The code must be production-ready and copy-pasteable.
Section 3: The "Why" (Education)
Explain the specific changes you made and why they are better. Use a "Before vs. After" comparison logic to help me learn.
Output everything in clean Markdown.Example Output
(This is what the user gets after running the prompt)
Section 1: The Code Review
Performance: The original code used nested loops O(n^2), which will crash with large datasets.
Readability: Variable names like x and data2 are vague and make maintenance impossible.
Safety: There was no null checking, likely causing runtime errors if the API response is empty.
Section 2: The Refactored Solution
TypeScript
/**
* Processes user data and filters active accounts.
* Uses a Set for O(1) lookups instead of O(n) arrays.
* @param {User[]} users - List of raw user objects
* @returns {ProcessedUser[]} - Sorted and filtered list
*/
const processActiveUsers = (users: User[]): ProcessedUser[] => {
if (!users || users.length === 0) return [];
try {
// Filter active users first to reduce processing load
return users
.filter((user) => user.isActive && user.email)
.map((user) => ({
id: user.id,
displayName: `${user.firstName} ${user.lastName}`,
lastLogin: new Date(user.lastLoginAt).toLocaleDateString(),
}))
.sort((a, b) => a.displayName.localeCompare(b.displayName));
} catch (error) {
console.error("Failed to process users:", error);
return [];
}
};
Section 3: The "Why"
Complexity Reduced: Changed the logic to a functional chain (filter -> map -> sort), which is easier to read and test than the original for loop.
Safety Added: Added a specific check for !users at the start to prevent "Cannot read property of undefined" errors.
Modern Typing: Enforced User[] types to catch bugs at compile time rather than runtime.