Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Code refactoring
on Cerebras Llama 3.1 70B

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

The "Vibe" Prompt

"Refactor this Python code for better readability and performance: ```python import os def process_data(file_path): data = [] with open(file_path, 'r') as f: for line in f: parts = line.strip().split(',') if len(parts) == 2: try: name = parts[0] value = int(parts[1]) data.append({'name': name, 'value': value}) except ValueError: print(f"Skipping invalid line: {line.strip()}") # Sort data by value and then filter sorted_data = sorted(data, key=lambda x: x['value']) filtered_data = [item for item in sorted_data if item['value'] > 10] # Calculate sum of values total_sum = sum(item['value'] for item in filtered_data) return filtered_data, total_sum # Example usage # file = 'data.txt' # if not os.path.exists(file): # with open(file, 'w') as f: # f.write('apple,5\nbanana,12\norange,7\n IndexError,abc\ngrape,20\n') # # result, s = process_data(file) # print(result, s) ```"
Low specificity, inconsistent output

Optimized Version

STABLE
You are an expert Python developer tasked with refactoring code. Your goal is to improve readability, maintainability, and performance. Follow these steps meticulously: **1. Understand the Goal:** The `process_data` function reads a CSV-like file, parses lines into dictionaries, filters data based on a numeric value, sorts it, and calculates a sum. **2. Identify Areas for Improvement (Think step-by-step):** * **Readability:** Can parsing logic be clearer? Can intermediate variables be more descriptive? Is error handling explicit? * **Performance:** Are there redundant iterations? Can sorting or filtering be optimized? Consider list comprehensions vs. generator expressions where appropriate. Avoid unnecessary data structure creation. * **Maintainability:** Break down complex operations into smaller, single-responsibility helper functions. Use constants for magic numbers. Add type hints. * **Standard Practices:** Incorporate Pythonic idioms. **3. Propose Specific Changes (Refactoring Plan):** * Introduce `csv.reader` for robust parsing. * Define a separate `_parse_line` helper function for clearer error handling and parsing of individual lines. Return `None` for invalid lines. * Use more descriptive variable names. * Combine filtering and sorting where feasible, or clarify their order. * Consider generator expressions for summing to reduce memory footprint for very large datasets if `filtered_data` itself isn't needed as a full list. (In this specific case, `filtered_data` is used as a list, so a separate sum is fine, but note the thought process). * Add type hints to function signatures and variables. * Convert `print` statements in error handling to `logging` for better production readiness. **4. Refactor the Code:** Apply the proposed changes to the provided Python codeblock. **Original Code:** ```python import os def process_data(file_path): data = [] with open(file_path, 'r') as f: for line in f: parts = line.strip().split(',') if len(parts) == 2: try: name = parts[0] value = int(parts[1]) data.append({'name': name, 'value': value}) except ValueError: print(f"Skipping invalid line: {line.strip()}") # Sort data by value and then filter sorted_data = sorted(data, key=lambda x: x['value']) filtered_data = [item for item in sorted_data if item['value'] > 10] # Calculate sum of values total_sum = sum(item['value'] for item in filtered_data) return filtered_data, total_sum # Example usage # file = 'data.txt' # if not os.path.exists(file): # with open(file, 'w') as f: # f.write('apple,5\nbanana,12\norange,7\n IndexError,abc\ngrape,20\n') # # result, s = process_data(file) # print(result, s) ``` **Refactored Code:** (Provide only the refactored code block, no additional comments or explanation outside the code itself.)
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt leverages Chain-of-Thought (CoT) by breaking the task into explicit, sequential steps: understanding the goal, identifying specific improvement areas, proposing a detailed refactoring plan, and finally, executing the refactoring. This approach forces the model to think systematically about the code's deficiencies and how to address them, leading to a more comprehensive and higher-quality refactoring. It guides the model to consider aspects like readability, performance, maintainability, and Pythonic practices, which are often overlooked in a naive prompt. The 'Refactoring Plan' section acts as a 'self-correction' or 'pre-computation' step, ensuring the model has a clear strategy before generating code. The final instruction to provide 'only the refactored code block' ensures concise and direct output.

0%
Token Efficiency Gain
The refactored code produced by the optimized prompt should use `csv.reader` for parsing.
The refactored code should incorporate type hints.
The refactored code should convert `print` statements during error handling to `logging`.

Ready to stop burning tokens?

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

Optimize My Prompts