Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Code refactoring
on Claude 3.5 Sonnet

Stop guessing. See how professional prompt engineering transforms Claude 3.5 Sonnet's output for specific technical tasks.

The "Vibe" Prompt

"Refactor this code to be better: [PASTE_CODE_HERE]"
Low specificity, inconsistent output

Optimized Version

STABLE
You are an expert software engineer specializing in code refactoring and best practices. Your goal is to improve the provided Python code snippet. Follow these steps meticulously: 1. **Analyze the current code:** Identify areas for improvement in terms of readability, maintainability, performance, and adherence to Pythonic conventions. Look for: * Redundant or duplicate code. * Complex logical structures (e.g., deeply nested loops/conditionals). * Lack of clear function/variable names. * Inefficient algorithms or data structures. * Missing or unclear comments/docstrings. * Potential for using built-in functions or standard library modules more effectively. * Violation of SOLID principles (if applicable to the snippet). * Error handling (or lack thereof). 2. **Propose specific refactoring strategies:** Based on your analysis, outline 2-3 concrete strategies you will employ. For example: "Extract function for `_process_data`", "Introduce meaningful variable names", "Replace nested loops with `comprehension`", "Implement early exit conditions", "Add type hints". 3. **Refactor the code:** Apply the proposed strategies to generate the improved code. Ensure the refactored code maintains the original functionality and passes all (implicit) existing tests. 4. **Provide a concise summary of changes:** Briefly explain the key improvements made and why they are beneficial. 5. **Identify potential next steps (optional):** Suggest any further enhancements that are outside the scope of this refactoring, but could be considered later. Here is the code to refactor: ```python [PASTE_CODE_HERE] ``` Your output should present the analysis, strategies, refactored code, summary, and next steps clearly.
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt works by providing a highly structured, multi-step chain-of-thought process. It explicitly defines the persona ('expert software engineer'), the goal ('improve code snippet'), and a detailed analytical framework. By breaking down the task into distinct steps (analyze, propose, refactor, summarize, next steps), it guides the model towards a more comprehensive and higher-quality output. It also explicitly lists common refactoring patterns and areas of improvement, acting as a cognitive checklist for the model. This reduces ambiguity and encourages a systematic approach, preventing the model from just making superficial changes.

0%
Token Efficiency Gain
The optimized prompt explicitly defines a persona.
The optimized prompt uses a multi-step chain-of-thought approach.
The optimized prompt provides specific criteria for analysis.

Ready to stop burning tokens?

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

Optimize My Prompts