AI Prompt for Automated Code Refactoring Recommendations

About Prompt

  • Prompt Type – Dynamic
  • Prompt Platform – ChatGPT, Grok, Deepseek, Gemini, Copilot, Midjourney, Meta AI and more
  • Niche – Specific AI Tools
  • Language – English
  • Category – Software Development
  • Prompt Title – AI Prompt for Automated Code Refactoring Recommendations

Prompt Details

## AI Prompt for Automated Code Refactoring Recommendations

**Prompt Type:** Dynamic

**Purpose:** Software Development – Automated Code Refactoring Recommendations within AI Tools

**Target Audience:** AI Platforms (generalized)

**Prompt Structure:**

“`
## Code Refactoring Recommendation Request

**1. Code Snippet:**
“`“`

**2. Programming Language:** (e.g., Python, Java, JavaScript, C++, etc.)

**3. AI Tool Context:** (e.g., TensorFlow, PyTorch, scikit-learn, etc.) Specify the AI/ML library or framework used in the code. This helps tailor the refactoring towards best practices within that specific ecosystem.

**4. Refactoring Goals:** Specify the desired improvements. Choose one or more from the list below, or provide your custom goals. Provide a short explanation for each chosen goal and its specific context within the code snippet.

* **Performance Optimization:** (e.g., Reduce execution time, minimize memory usage) – Explain which parts of the code are performance bottlenecks and how you expect refactoring to improve them.
* **Readability/Maintainability:** (e.g., Improve code clarity, simplify complex logic, enhance code structure) – Specify which parts of the code are difficult to understand or maintain and how refactoring can address these issues.
* **Code Reusability/Modularity:** (e.g., Extract reusable functions/classes, reduce code duplication) – Indicate sections of code that could be generalized and reused in other parts of the project.
* **Error Handling/Robustness:** (e.g., Implement better error handling mechanisms, improve input validation) – Highlight potential error scenarios and suggest how refactoring can mitigate them.
* **Specific AI Tool Best Practices:** (e.g., Optimize tensor operations, use appropriate data structures for AI workloads) – Explain how refactoring can leverage the features and best practices of the specified AI tool (e.g., using vectorized operations in NumPy, optimizing graph execution in TensorFlow).
* **Custom Goal:** (e.g., Adherence to specific coding style guide, preparation for deployment to a specific environment) – Provide a clear description of the custom goal and its relevance to the code snippet.

**5. Constraints:** (Optional) Specify any constraints or limitations for the refactoring process.
* **Maximum Code Changes:** (e.g., Limit the number of lines changed or files modified)
* **Specific Libraries/Functions Allowed/Disallowed:** (e.g., Restrict the use of certain libraries or functions)
* **Compatibility Requirements:** (e.g., Maintain compatibility with specific hardware or software versions)

**6. Desired Output Format:** Specify the desired output format for the refactoring recommendations.
* **Refactored Code:** Provide the complete refactored code snippet.
* **Patch/Diff:** Generate a patch or diff that shows the changes between the original and refactored code.
* **Explanation of Changes:** Provide a detailed explanation of the changes made and their rationale.
* **Performance Metrics (if applicable):** If performance optimization is a goal, provide metrics comparing the performance of the original and refactored code.

**Example:**

“`
## Code Refactoring Recommendation Request

**1. Code Snippet:**
“`python
# … (insert Python code snippet using TensorFlow here) …
“`

**2. Programming Language:** Python

**3. AI Tool Context:** TensorFlow 2.x

**4. Refactoring Goals:**
* **Performance Optimization:** The nested loops in the `process_data` function are slow. Refactoring should aim to vectorize these operations using TensorFlow’s tensor operations to improve execution speed.
* **Readability/Maintainability:** The variable naming is inconsistent and the code lacks comments. Refactoring should improve the naming conventions and add clear comments to enhance readability.

**5. Constraints:** None

**6. Desired Output Format:**
* Refactored Code
* Explanation of Changes
* Performance Metrics (execution time comparison)
“`

**Dynamic Prompting Best Practices:**

This prompt is designed to be dynamic and adaptable. You can adjust the following parameters to tailor it to your specific needs:

* **Code Snippet:** Insert the code you want to refactor.
* **Programming Language:** Specify the programming language of the code.
* **AI Tool Context:** Indicate the relevant AI tool/library.
* **Refactoring Goals:** Select relevant goals and customize them with explanations.
* **Constraints:** Add any constraints specific to your project.
* **Desired Output Format:** Specify the format you want for the recommendations.

By using this structured and detailed prompt, you can effectively leverage the power of AI to generate tailored refactoring recommendations that enhance the quality and efficiency of your AI-related code.
“`