Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Code refactoring
on Mistral Large 2

Stop guessing. See how professional prompt engineering transforms Mistral Large 2'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 a senior software engineer specializing in code refactoring. Your task is to refactor the provided JavaScript code snippet to improve its readability, maintainability, and performance, while ensuring all existing functionalities are preserved. Here's an example of your thought process: 1. **Analyze the original code:** Identify areas for improvement like long functions, duplicated logic, unclear variable names, inefficient algorithms, or potential side effects. 2. **Propose specific refactoring steps:** Outline a plan, e.g., 'Extract function for X', 'Rename variable Y to Z', 'Implement early exit for W', 'Use array method A instead of loop B'. 3. **Explain the 'why' for each step:** Justify how each refactoring step contributes to the goals (readability, maintainability, performance). 4. **Provide the refactored code:** Present the final polished code. 5. **Summarize improvements:** Briefly list the key benefits achieved. Here is the code to refactor: ```javascript [PASTE CODE HERE] ``` Please provide your detailed refactoring analysis, step-by-step plan with justifications, the refactored code, and a summary of improvements following the thought process described above.
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt leverages a chain-of-thought approach, instructing the model to act as an expert, analyze, plan, justify, and then execute the refactoring. This structured guidance helps the model break down the complex task into manageable steps, leading to more comprehensive, accurate, and well-explained refactoring. It provides clear expectations for the output format, moving beyond a simple 'do this' instruction to a 'think like this and then do this' approach.

0%
Token Efficiency Gain
The optimized_prompt will consistently produce more detailed and well-justified refactorings than the vibe_prompt.
The optimized_prompt will lead to refactored code that is easier for a human to understand due to the included explanations.
The optimized_prompt will likely catch more subtle code smells and apply more sophisticated refactoring techniques.

Ready to stop burning tokens?

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

Optimize My Prompts