Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Code refactoring
on GPT-4o

Stop guessing. See how professional prompt engineering transforms GPT-4o's output for specific technical tasks.

The "Vibe" Prompt

"Refactor this code to be better. Make it more readable and efficient. Fix any bugs."
Low specificity, inconsistent output

Optimized Version

STABLE
You are an expert software engineer specializing in code refactoring. Your task is to refactor the provided Python code snippet. Follow these steps meticulously: 1. **Analyze and Understand:** Carefully read the provided code, identify its purpose, and note any existing issues (e.g., readability, efficiency, potential bugs, maintainability). 2. **Identify Refactoring Opportunities:** Based on your analysis, list specific areas for improvement. Categorize them by priority (High, Medium, Low). 3. **Propose Refactoring Strategy:** For each High-priority item, briefly describe your proposed refactoring approach (e.g., 'Extract method for X', 'Replace loop with list comprehension', 'Use f-strings for string formatting', 'Apply Guard Clauses'). 4. **Implement Refactored Code:** Provide the complete refactored code block. Ensure it adheres to PEP 8 guidelines and best practices for Python. 5. **Explain Changes:** After the refactored code, provide a concise explanation for each significant change made, directly linking it to the identified opportunities and the benefits achieved (e.g., 'Improved readability by extracting repeated logic into `calculate_discount` function', 'Enhanced efficiency by using a dictionary lookup instead of linear search'). 6. **Verify Correctness:** Briefly confirm that the refactored code maintains the original functionality and does not introduce new bugs. **Code to Refactor:** ```python [PASTE_CODE_HERE] ```
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt works significantly better due to several factors: 1. **Role Assignment:** 'You are an expert software engineer specializing in code refactoring' primes the model for a specific, high-quality output. 2. **Structured Steps (Chain-of-Thought):** It breaks down the complex task into manageable, sequential steps. This forces the model to think systematically, reducing omissions and improving the quality of analysis before generating code. 3. **Clear Objectives:** Each step has a clear objective (Analyze, Identify, Propose, Implement, Explain, Verify). 4. **Specific Refactoring Categories:** Requesting identification and categorization of opportunities guides the model to look for common refactoring patterns. 5. **Explicit Best Practices:** Mentioning PEP 8 and best practices ensures adherence to coding standards. 6. **Detailed Explanation Requirement:** Asking for explanations of changes forces the model to justify its decisions, making the output more transparent and educational. 7. **Verification Step:** The final verification step encourages the model to 'self-critique' and confirm correctness. 8. **Reduced Ambiguity:** The naive prompt is highly ambiguous ('better', 'more readable', 'efficient', 'fix any bugs') and leaves too much interpretation to the model, leading to inconsistent or incomplete results. The optimized prompt provides concrete actions and expected outputs.

0%
Token Efficiency Gain
The refactored code should always be provided within a markdown code block.
The model should list specific refactoring opportunities before implementing changes.
The explanation of changes should directly follow the refactored code.

Ready to stop burning tokens?

Join 5,000+ developers using Prompt Optimizer to slash costs and boost LLM reliability.

Optimize My Prompts