Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Code refactoring
on Groq Llama 3.1 70B

Stop guessing. See how professional prompt engineering transforms Groq Llama 3.1 70B's output for specific technical tasks.

The "Vibe" Prompt

"Refactor this code. Make it better."
Low specificity, inconsistent output

Optimized Version

STABLE
You are an expert software engineer with extensive experience in code refactoring and optimization. Your task is to refactor the provided JavaScript code snippet to improve its readability, maintainability, performance, and adherence to best practices. **To achieve this, follow these steps in a thinking process:** 1. **Understand the Current Code:** Analyze the input code snippet thoroughly. Identify its purpose, key functionalities, and any existing issues (e.g., redundancy, poor naming, inefficiencies, clarity problems, potential bugs). 2. **Identify Refactoring Opportunities:** Based on your understanding, pinpoint specific areas that can be improved. Categorize these improvements (e.g., extracting functions, simplifying conditions, better variable names, using modern syntax, optimizing loops, error handling). 3. **Propose Initial Refactoring Strategy:** Outline a high-level plan for how you will refactor the code. Which improvements will you tackle first? What's the logical order of operations? 4. **Execute Refactoring (Step-by-step):** Apply the refactoring changes iteratively. For each major change, briefly explain *why* you are making it and *what* improvement it brings. 5. **Review and Verify:** After refactoring, review the new code. Ensure it still meets the original functional requirements, is more readable, maintainable, and performs as expected. Verify that no new bugs have been introduced. **Constraints:** * Your refactored code must maintain the original functionality. * Only provide the refactored code block. Do not include introductory or concluding remarks outside the thought process. * If helper functions are created, they should be clearly defined and scoped. * Focus on idiomatic JavaScript practices. **Input Code Snippet:** ```javascript // USER_PROVIDED_CODE_GOES_HERE ``` **Thinking Process and Refactored Code:**
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt leverages several techniques to make 'Groq Llama 3.1 70B' more effective for code refactoring: 1. **Role Assignment (Expert Software Engineer):** Establishes context and expectations for the model's persona, guiding it to think like an expert. 2. **Clear Goal Definition:** Explicitly states the desired outcomes: readability, maintainability, performance, adherence to best practices. 3. **Chain-of-Thought (CoT):** Breaks down the complex task into a sequence of logical, manageable steps (Understand, Identify, Propose, Execute, Review). This encourages the model to 'think aloud' and structure its internal reasoning process. 4. **Specific Sub-tasks within CoT:** Each CoT step has detailed instructions, pushing the model to consider various aspects of refactoring (e.g., 'redundancy, poor naming, inefficiencies' for understanding; 'extracting functions, simplifying conditions' for identifying opportunities). 5. **Justification Requirement:** Asking 'why' for each refactoring change means the model not only performs the action but also articulates its rationale, leading to more intentional and higher-quality refactoring. 6. **Constraints:** Explicitly defines boundaries (maintain functionality, only refactored code, idiomatic JavaScript), preventing undesirable outputs. 7. **Input Placeholder:** Clearly shows where the user's code should be inserted. 8. **Output Format Hint:** The 'Thinking Process and Refactored Code:' header guides the model on how to present its output, encouraging a structured response that includes the CoT. In contrast, 'Refactor this code. Make it better.' is extremely vague, offering no guidance on what 'better' means, what approach to take, or what considerations are important. This leads to inconsistent and often superficial refactoring from the model.

0%
Token Efficiency Gain
The optimized prompt consistently produces more comprehensive and insightful refactoring suggestions compared to the naive prompt.
The optimized prompt ensures that the refactored code maintains original functionality due to the explicit constraint.
The optimized prompt generates explanations for refactoring choices, which is absent in responses from the naive prompt.

Ready to stop burning tokens?

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

Optimize My Prompts