Get a prioritized list of independent, behavior-preserving refactoring suggestions for a code snippet, each with before/after code, rationale and a verification step.
Review the supplied code and return separate refactoring suggestions that a developer can accept or reject one at a time. Refactoring must not change observable behavior; call out any suggestion that would. ## Inputs Code and language/version: [CODE] What the code is responsible for and who calls it: [CONTEXT] Constraints (public API that must not change, performance budget, style guide): [CONSTRAINTS] Existing tests, if any: [TESTS] ## Instructions Look for, in this order of priority: 1. Duplicated logic that can be extracted into a function or data table. 2. Complex conditionals that can be simplified (guard clauses, lookup tables, polymorphism) without changing branch results, including the default branch. 3. Names that hide intent. 4. Loops that recompute values or use an unsuitable data structure; only claim a performance gain when the change in work is explainable. 5. Design patterns, only where they reduce complexity for this code rather than add structure. Apply DRY, KISS and YAGNI as tie-breakers, not as reasons on their own. ## Return A numbered list of suggestions. Each suggestion contains: - **Suggestion ID** - **Priority**: high, medium or low, with the reason - **Description** - **Code Snippet**: the original lines - **Refactored Code** - **Rationale** - **Behavior check**: the test or input that shows behavior is unchanged - **Risk**: anything that could change, such as error types, ordering or None/null handling ## Acceptance checks - Each refactored snippet is a drop-in replacement for its original snippet. - Suggestions that change behavior are labeled and not mixed with pure refactorings. - Every suggestion has a concrete behavior check. - If the code is too short or already clear, return fewer suggestions rather than padding the list.
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Python 3.12:
def shipping_cost(vehicle, distance_km):
if vehicle == "bike":
cost = distance_km * 0.5
return round(cost + 2, 2)
elif vehicle == "van":
cost = distance_km * 1.2
return round(cost + 2, 2)
elif vehicle == "truck":
cost = distance_km * 2.0
return round(cost + 2, 2)
Context: called by the checkout service; unknown vehicles currently return None
Constraint: function name and signature must not change
Tests: noneSuggestions such as: (1) replace the if/elif chain with a RATE_PER_KM dictionary and a single round(distance_km * rate + 2, 2) expression; (2) name the constant 2 as BASE_FEE. The behavior check for (1) covers each vehicle type and an unknown vehicle, and the Risk field notes that unknown vehicles must still return None rather than raising KeyError unless the team chooses to change that behavior as a separate, labeled change. Illustrative expectation, not a recorded model run.
Ask questions or share concrete improvements.
No comments yet.
Sign in to join the discussion.
Guides for adapting and checking prompts: