How do I get the AI to explain what its code does?
Effective Prompts for Explanations
Instead of just asking 'What does this code do?', ask for a line-by-line breakdown or a summary of the main logic. You can also request that the AI add comments to the code.
For example: 'Explain this function in plain English, describing each step and why it's needed.' Or: 'Add comments to this code and then summarize its purpose.'
- 'Explain this code line by line.'
- 'What is the purpose of this function?'
- 'Add comments to this code and then give a high-level summary.'
- 'What are the inputs and outputs of this code?'
- 'Are there any potential edge cases or bugs in this code?'
Follow-Up Questions
After the initial explanation, ask follow-up questions about specific parts you don't understand. For example: 'Why did you use a while loop here instead of a for loop?' or 'What happens if the input is null?'
You can also ask the AI to rewrite the code in a simpler way or to explain it as if you're a beginner.
Using Explanations to Learn
Explanations can help you learn new patterns or libraries. But remember that AI might occasionally be wrong, so cross-check with official documentation.
If the explanation seems off, ask the AI to clarify or provide references. You can also ask it to generate tests that demonstrate the code's behavior.
Common mistakes
- Asking for an explanation without specifying the desired depth, leading to vague answers.
- Not asking follow-up questions when parts of the explanation are unclear.
- Trusting the explanation without verifying against documentation or tests.
