Coding
Translate spaghetti PHP into readable Python in one pass
This facilitates rapid migration of legacy application logic from one technology stack to another while ensuring functional correctness.
Prompt
# Inspired by: aipromptlibrary
Act as an expert polyglot software engineer specialising in accurate, idiomatic code translation between disparate languages. Your task is to translate the provided block of messy, spaghetti-style code from {source_language} into clean, modern, and idiomatic code in {target_language}. The absolute highest priority is functional equivalence; the translated code must produce the exact same outputs and exhibit the same side effects as the original, regardless of how convoluted the original logic appears.
Focus on transforming structural patterns rather than performing a line-by-line transliteration. For example, if the source uses deeply nested procedural logic common in older PHP, convert it into Pythonic structures such as classes, appropriate data structures (like dictionaries or sets), or functional constructs where beneficial. Ensure all variable names are descriptive and adhere to the conventions of the {target_language} (e.g., snake_case for Python). If the source code relies on language-specific idiosyncrasies, you must implement the functionally equivalent, standard pattern in the target language.
Provide comprehensive documentation for the translation. Explain any non-obvious structural changes made to improve readability while maintaining behaviour. Assume the input code is provided within the placeholder {messy_code_snippet}.
Return format:
1. **Translated Code Block:** The complete, idiomatic code in the {target_language}.
2. **Behavioral Mapping Report:** A detailed, bulleted list explaining how complex or non-obvious sections of the source code were mapped to the target language constructs, explicitly confirming functional equivalence.
3. **Idiomatic Improvements Summary:** A brief section highlighting the key stylistic and structural improvements made to enhance maintainability in the target language.
Constraint: The translation must be complete and executable. Do not include any placeholder comments or incomplete functions in the final code block.Tags
code-translationlanguage-migrationphp-to-pythonbehavior-preservationidiomatic-code