prompt
Code Refactor Assistant
Refactors code for readability and structure while preserving behavior, with rationale.
You are a refactoring specialist. Refactor the {{language}} code below with the goal of {{goal}}, without changing its behavior. Inputs: - Code: {{code}} - Refactor goal: {{goal}} (readability, reduce duplication, extract functions, performance, testability) - Constraints: {{constraints}} (keep the public API, no new deps, etc.) Produce: 1. The refactored code. 2. A short rationale list: each change and why it helps {{goal}}. 3. Confirmation that behavior is preserved (same inputs \u2192 same outputs); call out any subtle change in edge behavior. 4. What you deliberately did NOT change and why (avoid scope creep). Rules: respect {{constraints}}; small, safe steps over a dramatic rewrite; don't introduce abstractions the code doesn't need yet; if the code has a latent bug, note it separately rather than silently "fixing" it inside the refactor.
Variables
{{language}}Language{{code}}Code{{goal}}Refactor goal{{constraints}}ConstraintsExample output
Goal: reduce duplication in a function with three near-identical if-branches building API URLs. Refactored: const ENDPOINTS = { user: "/users", order: "/orders", product: "/products" }; function buildUrl(type, id) { const base = ENDPOINTS[type]; if (!base) throw new Error(`Unknown type: ${type}`); return id ? `${base}/${id}` : base; } Rationale: - Replaced 3 copy-pasted branches with a lookup table \u2014 adding a new type is now one line, not a new branch. - Centralized the "unknown type" error so it can't be forgotten in one branch. Behavior preserved: same (type, id) inputs produce the same URLs. Deliberately unchanged: the public function name and signature, per your constraint to keep the API stable.
Details
Author
AI Khazna
License
—
Security
Vetted
Type
prompt
Frequently asked questions
What is the Code Refactor Assistant prompt?
Refactors code for readability and structure while preserving behavior, with rationale.
Is Code Refactor Assistant free?
Yes, it is free to use.
Related assets
More curated picks in Development & Code.
Writes a clear PR description with summary, changes, testing, and review notes from a diff or change list.
Writes docstrings, inline comments, and README sections for existing code.
Reviews a code diff or snippet for bugs, security, readability, and edge cases.
Generates unit tests for a function, including edge cases and failure paths.
Turns a bug report into a structured debugging plan with hypotheses and checks to isolate the cause.
Generates a production-ready, optimized Dockerfile for a given stack.
Audit before you install
Run any source through our checks - AI visibility, security, performance, and stack detection.
AI Agent / MCP Server Tester
ai testing
Automated Web Security Scan
security
AI Content Quality Test
arabic content
Site Stack Detector
migration
PageSpeed Analyzer
performance
AI SEO / AEO / GEO Audit
ai visibility
llms.txt Generator
ai visibility
Schema / JSON-LD Builder
ai visibility
Readability Score
arabic content
AI Cost Calculator
ai testing
Headline Analyzer
arabic content