Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Code refactoring
on Command R+

Stop guessing. See how professional prompt engineering transforms Command R+'s output for specific technical tasks.

The "Vibe" Prompt

"Refactor this code to make it better:"
Low specificity, inconsistent output

Optimized Version

STABLE
You are an expert software engineer specializing in code refactoring. Your task is to refactor 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 best practices. Specifically look for: * Redundancy (duplication of code). * Poor naming conventions for variables, functions, or classes. * Violation of Single Responsibility Principle (SRP). * Complex control flow (nested loops, deeply indented blocks). * Lack of comments or unclear comments. * Potential for using built-in functions or more idiomatic Python. * Inefficient algorithms or data structures. 2. **Propose specific refactoring strategies:** Based on your analysis, suggest concrete changes. For each suggested change, explain *why* it is an improvement. Examples of strategies include: * Extracting functions/methods. * Renaming variables/functions. * Replacing magic numbers with named constants. * Simplifying conditional expressions. * Using list comprehensions or generator expressions. * Applying design patterns (if applicable and beneficial). * Adding type hints. 3. **Implement the refactored code:** Provide the complete refactored code block. Ensure it is syntactically correct and addresses the identified issues. 4. **Provide a concise summary of changes:** Briefly list the key improvements made and their impact. **Original Code:** ```python # [INSERT ORIGINAL CODE HERE] ``` **Refactoring Goal:** Improve overall code quality and make it easier to understand and maintain.
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt leverages chain-of-thought by breaking down the complex task of 'code refactoring' into smaller, manageable, and sequential steps. It explicitly instructs the model on what to analyze, what to propose, how to implement, and how to summarize. This structured approach guides the model's reasoning process, ensuring it covers all essential aspects of refactoring. By providing concrete examples of refactoring strategies and expected improvements, it primes the model for high-quality output. The use of clear headings and bullet points makes the instructions unambiguous. It also defines the 'persona' of an 'expert software engineer', which often elicits higher quality and more detailed responses from large language models.

0%
Token Efficiency Gain
The optimized prompt explicitly asks for identifying specific improvement areas.
The optimized prompt requires proposing and explaining refactoring strategies.
The optimized prompt mandates providing the full refactored code.

Ready to stop burning tokens?

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

Optimize My Prompts