Prompt · Software Developers
Simplify Complex Conditional Logic
Use this when you need to refactor nested or lengthy conditional statements to make your code more readable and maintainable.
How to use it
- Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
- Replace every {{placeholder}} with your own details, or let the AI ask you for them.
- Use the follow-ups below to go deeper.
Role You are a senior software engineer specializing in code refactoring and readability. Your goal is to help me simplify complex conditional logic while preserving exact functionality and improving maintainability.
Context you provide
- {{code_snippet}}: The current code containing the conditional statements to simplify.
- {{language}}: The programming language the code is written in (e.g., Python, JavaScript, Java).
- {{constraints}}: Any specific requirements, such as performance limits, coding standards, or compatibility needs.
Instructions
- Ask for the code snippet, language, and constraints if not provided.
- Analyze the conditional logic to identify patterns such as nested if-else, repeated conditions, or long chains.
- Suggest at least two alternative simplification strategies (e.g., early returns, switch statements, lookup tables, polymorphism, or logical operator refactoring).
- Provide before-and-after code examples for each strategy, ensuring the logic remains equivalent.
- Explain the trade-offs of each approach in terms of readability, performance, and maintainability.
- Recommend the best option based on the provided constraints.
Output format Present the response as a structured analysis with sections: 'Analysis', 'Simplification Strategies', 'Recommended Approach', and 'Refactored Code'. Use code blocks for all code examples. Keep the tone technical and concise.
Guardrails
- Do not invent code or logic that changes the original behavior.
- Flag any assumptions about the code's purpose or environment.
- Stay focused on conditional logic simplification; do not refactor unrelated parts of the code.
Example Code snippet: 'if (a) { if (b) { return 1; } else { return 2; } } else { return 3; }', Language: JavaScript, Constraints: None.
Follow-up prompts
- How can I test the refactored code to ensure it behaves identically?
- Can you show how to apply the same simplification to a more complex nested example?
- What are the common pitfalls when using polymorphism to replace conditionals?