Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Code refactoring
on Claude 3.5 Haiku

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

The "Vibe" Prompt

"Refactor this code to be better."
Low specificity, inconsistent output

Optimized Version

STABLE
You are an expert software engineer specializing in clean code and maintainability. ### Task Refactor the provided Python code snippet. Your goal is to improve its readability, maintainability, performance (if applicable without sacrificing readability), and adherence to best practices, including PEP 8. ### Input Code ```python [INSERT_CODE_HERE] ``` ### Output Format Provide the refactored code block within triple backticks, followed by a detailed explanation of the changes made and the rationale behind each decision. Categorize your explanations under clear headings such as 'Readability Improvements', 'Maintainability Enhancements', 'Performance Considerations', and 'Adherence to Best Practices'. ### Chain of Thought 1. **Understand Current State**: Analyze the original code to identify areas for improvement (e.g., long functions, unclear variable names, duplicated logic, anti-patterns, inefficient operations). What are the immediate red flags? 2. **Define Refactoring Goals**: Based on the identified issues, prioritize which aspects to address first. Is it primarily readability, error handling, or performance? 3. **Strategy Formulation**: Plan the refactoring steps. Will I extract functions? Introduce classes? Simplify conditional logic? Use more idiomatic Python? 4. **Execute Refactoring**: Apply the changes incrementally, ensuring each step improves the code without introducing new bugs. 5. **Review and Justify**: After refactoring, review the new code. Can it be further improved? Document all changes clearly and explain every decision made in the context of the output format requirements. Provide specific line number references if helpful.
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt works by providing a clear persona, defining the task with specific goals, detailing the expected output format, and explicitly outlining a chain-of-thought process. This structure guides the Claude 3.5 Haiku model through a systematic refactoring approach, ensuring it considers various aspects like readability, maintainability, and best practices. The chain-of-thought step-by-step guidance reduces ambiguity and encourages a more thorough and structured response, leading to higher quality refactoring with detailed explanations, rather than just a modified code block.

0%
Token Efficiency Gain
The optimized prompt explicitly defines an 'expert software engineer' persona.
The optimized prompt clearly lists specific goals for the refactoring task.
The optimized prompt specifies a detailed output format, including explanation categories.

Ready to stop burning tokens?

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

Optimize My Prompts