Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Code refactoring
on Phi-3.5 MoE

Stop guessing. See how professional prompt engineering transforms Phi-3.5 MoE's output for specific technical tasks.

The "Vibe" Prompt

"Refactor this Python code for better readability and performance: ```python def process_data(data_list): result = [] for item in data_list: if 'value' in item and item['value'] > 10: processed_item = item['value'] * 2 result.append(processed_item) return result ```"
Low specificity, inconsistent output

Optimized Version

STABLE
You are an expert Python developer specialized in writing clean, performant, and idiomatic Python code. Your task is to refactor the provided Python function. Follow these steps meticulously: 1. **Understand the Current Logic**: Analyze the existing function to identify its core purpose, inputs, and outputs. 2. **Identify Areas for Improvement (Readability)**: Pinpoint sections where code clarity can be enhanced. Consider aspects like variable naming, conditional complexity, and loop structure. 3. **Identify Areas for Improvement (Performance)**: Determine if there are any obvious performance bottlenecks or opportunities for more efficient data handling or algorithmic choices. 4. **Propose Refactoring Changes**: Systematically suggest specific changes for both readability and performance. * For readability, explain *why* each change improves clarity. * For performance, explain *why* each change is more efficient. 5. **Implement the Refactored Code**: Provide the complete, refactored Python function. 6. **Add a Brief Explanation of Changes**: Summarize the key improvements made. Here is the code to refactor: ```python def process_data(data_list): result = [] for item in data_list: if 'value' in item and item['value'] > 10: processed_item = item['value'] * 2 result.append(processed_item) return result ```
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt leverages several techniques to improve the quality of the output. First, it establishes the model's persona as an 'expert Python developer,' setting a high standard for the response. Second, it uses a Chain-of-Thought (CoT) approach by breaking down the task into explicit, numbered steps. This guides the model through a logical reasoning process, ensuring it understands the code, identifies issues, proposes solutions, and then implements them. The detailed instructions for each step (e.g., 'explain *why* each change improves clarity') force the model to justify its decisions, leading to more insightful and actionable refactoring. This structured approach reduces ambiguity and the likelihood of omissions, resulting in a more comprehensive and higher-quality refactored solution with clear explanations. The user receives not just refactored code, but also an understanding of *why* those changes were made.

0%
Token Efficiency Gain
The 'vibe_prompt' should produce refactored code, but without detailed explanations or justification for changes.
The 'optimized_prompt' must generate refactored code accompanied by a step-by-step breakdown as requested in the CoT instructions.
The 'optimized_prompt' output should include explicit reasoning for both readability and performance improvements.

Ready to stop burning tokens?

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

Optimize My Prompts