Coding
AI Prompt for Pattern-Based Refactoring
Apply design patterns correctly with this AI prompt: identify the fitting pattern, refactor your code to it, and learn when NOT to use patterns.
The prompt
Role: You are a software design expert who applies patterns pragmatically — only where they genuinely help. Context: Language: [LANGUAGE]. Code: [CODE]. Goal: [GOAL, e.g. easy to add new payment methods, decouple UI from logic]. Task: 1) Recommend the SINGLE best-fitting pattern for [GOAL] (Strategy, Factory, Observer, etc.) and explain WHY it fits — what pain does it remove? 2) Consider and reject one alternative pattern explicitly (why not it?). 3) Refactor my code to the pattern, showing the complete result. 4) Map pattern roles to my classes/functions. 5) State honestly when this pattern would be OVERKILL and what simpler approach to use instead. Format: Recommendation with reasoning, then the rejected alternative, then refactored code, then the role mapping, then the overkill warning. Ask the AI to show the cost of the pattern too: new classes, indirection, and the learning curve for juniors. Patterns are investments — the prompt's overkill warning keeps the accounting honest. Constraints: One pattern, applied well — not a pattern showcase. Keep the refactor minimal; do not restructure unrelated code. Preserve existing behavior.
Variables to fill in
Replace each [variable] in the prompt with your own details:
[LANGUAGE] [CODE] [GOAL]
Example input
LANGUAGE=Python, CODE=payment processing with if/elif chains for card/paypal/crypto, PATTERN=Strategy, GOAL=easy to add new payment methods
How to use this prompt
State the goal (what should become easy) — the pattern choice follows from it. Ask for the justification; patterns applied without reason become complexity.
Tips for the best results
- Read the 'rejected alternative' — it teaches pattern selection better than the recommendation.
- Ask for the overkill warning seriously; most pattern misuse is over-application.
- Have it show the before/after for adding ONE new variant to prove the pattern pays off.
Frequently asked questions
Do I really need a design pattern?
It applies the pattern minimally to your code and justifies the choice — including when a simpler approach beats the pattern.
How do I know I chose the right pattern?
The prompt makes you reject one alternative explicitly. If you cannot articulate why the alternative loses, you do not understand the choice yet.