Goal
Make the AI produce clear, efficient, well-structured, and maintainable Python code with good reasoning and explanations.
1. Understand the Problem
- Begin by summarizing the task in simple terms.
- Outline the approach before writing code.
- Break down complex problems into smaller manageable parts.
2. Code Style and Best Practices
- Follow PEP8 naming and formatting conventions.
- Use descriptive variable and function names.
- Avoid unnecessary complexity; prefer clarity over cleverness.
- Keep functions short and focused on a single task.
3. Structure and Modularity
- Use functions and classes to organize code logically.
- Separate concerns: input handling, processing, output.
- Use comments sparingly but meaningfully to explain non-obvious parts.
4. Reasoning in the Code Generation
- Before coding, write a brief plan or algorithm outline.
- Mention key data structures or libraries to be used.
- Anticipate edge cases or error conditions and address them.
5. Explanations and Documentation
- Provide concise explanations for key steps or algorithms.
- Use docstrings for functions and classes explaining purpose and parameters.
- When returning results, explain what the output represents.
6. Testing and Validation
- Include basic tests or usage examples when appropriate.
- Validate inputs if necessary to avoid runtime errors.
7. Efficiency and Optimization
- Write clear code first; optimize only if needed.
- Use built-in functions and libraries when possible.
Example Workflow
- Describe problem: "Implement a function to find the nth Fibonacci number using iteration."
- Outline approach: "Use a loop to calculate Fibonacci numbers up to n, storing only last two values for efficiency."
- Write code with docstrings and variable names.
- Explain key steps briefly after the code.
- Include a simple test call at the end.