Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Code refactoring
on Gemini 1.5 Pro

Stop guessing. See how professional prompt engineering transforms Gemini 1.5 Pro's output for specific technical tasks.

The "Vibe" Prompt

"Refactor this code to be better. Make it more readable and efficient. Here's the code: [CODE]"
Low specificity, inconsistent output

Optimized Version

STABLE
You are an expert software engineer specializing in code refactoring, adhering to best practices and principles of clean code. Your task is to refactor the provided Python code snippet. **Goal:** Improve code readability, maintainability, and efficiency. **Specific Refactoring Areas to Consider (prioritize in order):** 1. **Clarity & Readability:** Rename ambiguous variables/functions, add concise comments where necessary, improve logical flow. 2. **Modularity & Reusability:** Break down complex functions, extract common logic, reduce coupling. 3. **Efficiency:** Identify and optimize potential performance bottlenecks (e.g., redundant computations, inefficient data structures). 4. **Error Handling (if applicable):** Suggest improvements for more robust error management. 5. **Pythonic Idioms:** Replace non-idiomatic code with Pythonic constructs. **Constraints:** * Do not change the core functionality or external API. * Explain your refactoring choices clearly. * Provide the refactored code block, followed by an explanation of changes. **Original Code:** ```python [CODE] ``` **Thought Process for Refactoring:** 1. **Analyze the original code:** Identify its purpose, current structure, and potential issues based on the refactoring areas. 2. **Prioritize refactoring steps:** Determine the most impactful changes first. 3. **Apply changes iteratively:** Implement one refactoring change at a time. 4. **Verify functionality (mentally):** Ensure each change preserves the original behavior. 5. **Document rationale:** Explain why each change was made and its benefit.
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt works by providing a highly structured, role-playing instruction to Gemini. It sets a clear persona ('expert software engineer'), defines specific and prioritized goals for refactoring, outlines constraints to prevent unintended alterations, and crucially, includes a 'Thought Process' section. This 'Thought Process' section acts as a chain-of-thought instruction, guiding Gemini through a methodical approach to analyzing, planning, and executing the refactoring, thereby leading to more comprehensive and relevant improvements. The explicit breakdown of refactoring areas ensures all common aspects are considered.

0%
Token Efficiency Gain
The optimized prompt clearly defines the persona and task.
The optimized prompt provides specific, prioritized refactoring areas.
The optimized prompt includes constraints for the refactoring task.

Ready to stop burning tokens?

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

Optimize My Prompts