The Prompt Library
CodingPython

Refactor Python Code for Clarity

Improve readability and structure of messy or inefficient Python code.

Select Model:

Prompt:

I need help refactoring some Python code to make it more readable, maintainable, and efficient. Please analyze the code and provide an improved version along with explanations of your changes. Original code: ```python [PASTE CODE HERE] ``` Please provide: 1. REFACTORED CODE * A complete, improved version of the original code * Code that maintains the exact same functionality * Clear, consistent naming conventions * Proper docstrings and comments * Logical organization into functions/classes * PEP 8 compliance 2. REFACTORING CHANGES EXPLAINED * Summary of the major improvements made * Explanation of each significant change and its purpose * Reasoning behind organizational or structural changes * Clarification of any complex techniques introduced 3. READABILITY IMPROVEMENTS * Better variable and function names * Clearer code structure and organization * More effective use of whitespace and formatting * Removal of redundant or duplicate code * Simplification of complex expressions 4. PERFORMANCE OPTIMIZATIONS * Identification of inefficient operations * Suggested algorithmic improvements * Better use of built-in functions and data structures * Reduction of unnecessary computations * Memory usage improvements (where applicable) 5. BEST PRACTICES IMPLEMENTATION * Proper error handling * Use of appropriate Python idioms * Type hints (if using Python 3.6+) * Separation of concerns * Better modularity * Improved testability Please ensure the refactored code is fully functional and equivalent to the original code in terms of inputs, outputs, and behavior. If you make assumptions about the original code's intent, please state them clearly.

Related Prompts

Other prompts you might find useful

CodingPython

Debug Python Error Message

Help diagnose and fix a Python error or traceback.

6 models availableView details
CodingPython

Python Script Generator

Generate a basic Python script for a task, function, or automation.

6 models availableView details
CodingPython

Explain a Python Code Snippet

Break down a piece of Python code and explain what it does in plain English.

6 models availableView details
CodingPython

Refactor Python Code for Clarity

Improve readability and structure of messy or inefficient Python code.

6 models availableView details