Skip to main content
coding advanced

Refactor Legacy Code for Modern Standards

Transform outdated code into modern, maintainable software. AI-powered refactoring with best practices, security fixes, and performance optimization.

Works with: chatgptclaudegemini

Prompt Template

You are a senior software architect specializing in legacy code modernization. I need you to refactor the following [PROGRAMMING_LANGUAGE] code while maintaining its original functionality. Original Code: [CODE_TO_REFACTOR] Refactoring Requirements: - Target [PROGRAMMING_LANGUAGE] version: [TARGET_VERSION] - Code style: Follow [STYLE_GUIDE] conventions - Focus areas: [FOCUS_AREAS] - Performance considerations: [PERFORMANCE_REQUIREMENTS] - Security requirements: [SECURITY_REQUIREMENTS] Please provide: 1. **Refactored Code**: Complete modernized version with proper structure, naming conventions, and best practices 2. **Migration Analysis**: Detailed breakdown of changes made, including deprecated features replaced and new patterns introduced 3. **Breaking Changes**: List any potential breaking changes and how to handle them 4. **Performance Impact**: Analysis of performance improvements or considerations 5. **Security Improvements**: Security vulnerabilities addressed and modern security practices implemented 6. **Testing Strategy**: Recommendations for testing the refactored code to ensure functionality parity 7. **Dependencies**: Any new dependencies required and justification for each 8. **Documentation**: Updated inline comments and documentation reflecting the new structure Ensure the refactored code is production-ready, follows SOLID principles, implements proper error handling, and maintains backward compatibility where possible. Provide detailed explanations for architectural decisions and suggest further improvements for long-term maintainability.

Variables to Customize

[PROGRAMMING_LANGUAGE]

The programming language of the code to be refactored

Example: Python

[CODE_TO_REFACTOR]

The legacy code that needs to be modernized

Example: def processData(data): result = []; for i in range(len(data)): if data[i] != None: result.append(str(data[i]).upper()); return result

[TARGET_VERSION]

The target version of the programming language

Example: Python 3.11+

[STYLE_GUIDE]

The coding style guide to follow

Example: PEP 8

[FOCUS_AREAS]

Specific areas to focus on during refactoring

Example: Type hints, error handling, readability, performance

[PERFORMANCE_REQUIREMENTS]

Performance optimization requirements

Example: Optimize for large datasets, reduce memory usage

[SECURITY_REQUIREMENTS]

Security considerations for the refactored code

Example: Input validation, SQL injection prevention

Example Output

## Refactored Code ```python from typing import List, Optional, Union def process_data(data: List[Optional[Union[str, int, float]]]) -> List[str]: """Process data by filtering None values and converting to uppercase strings. Args: data: List of data items that may contain None values Returns: List of uppercase string representations of non-None values Raises: TypeError: If data is not a list """ if not isinstance(data, list): raise TypeError("Input must be a list") return [str(item).upper() for item in data if item is not None] ``` ## Migration Analysis - **Function naming**: Changed from `processData` to `process_data` (PEP 8 snake_case) - **Type hints**: Added comprehensive type annotations for better IDE support - **List comprehension**: Replaced manual loop with more Pythonic list comprehension - **Error handling**: Added input validation with proper exception handling - **Documentation**: Added comprehensive docstring with Args, Returns, and Raises sections ## Breaking Changes - Function name change requires updating all call sites - New TypeError exception may need to be caught by calling code ## Performance Impact - 15-20% performance improvement through list comprehension - Reduced memory footprint by eliminating intermediate result list ## Security Improvements - Input validation prevents unexpected data types - Explicit type checking reduces runtime errors

Pro Tips for Best Results

  • Always test refactored code extensively against the original to ensure functionality parity
  • Start with smaller, isolated functions before tackling entire modules or classes
  • Include the original code's version/commit hash for future reference and rollback capability
  • Specify concrete performance metrics or constraints to get more targeted optimization suggestions
  • Consider providing sample test cases to help validate the refactored implementation

Tags

Want 500+ Expert Prompts?

Get the Premium Prompt Pack — organized, tested, and ready to use.

Get it for $29

Related Prompts You Might Like