Refactor code prompt: cleaner code in one pass
Paste any function or file and get a refactored version that's easier to read and maintain, plus a note on what changed and why.
متى تستخدم هذا البرومبت
- When a function has grown messy over time.
- Before a code review or handoff.
- When onboarding new team members to a file.
نص البرومبت
Refactor the following {{language}} code to improve readability, reduce complexity, and follow {{framework}} best practices. Keep behavior identical and explain each change briefly.
{{CLIPBOARD}}{{language}}{{framework}}{{CLIPBOARD}}الكتلة {{CLIPBOARD}} تُستبدل تلقائياً بما نسخته قبل الضغط على «نسخ».
كيف تستخدمه
- انسخ البرومبت بالزر أو املأ المتغيرات أولاً.
- الصقه في ChatGPT أو Claude أو Gemini.
- عدّل النتيجة أو أعد الطلب بتغيير المتغيرات.
مثال على النتيجة
Refactored version:
function getActiveUsers(users) {
return users.filter(u => u.isActive);
}
Changes:
- Removed nested loop, replaced with filter
- Renamed arr to users for clarity
- Extracted magic number into named constantنصائح للاستخدام
- Specify the language and framework for idiomatic suggestions.
- Ask for a diff-style output if you only want the changed lines.
- Run the existing test suite after refactoring to confirm behavior is unchanged.
النماذج الموصى بها
Claude
أسئلة شائعة
- Will this change what the code does?
- No, the prompt explicitly asks to preserve behavior; always re-run your tests to confirm.
- Can I use this on a whole file, not just a function?
- Yes, paste the full file; for very large files, refactor one function at a time for clearer results.