Sub topic 1
Start with a Specific Question
Prerequisites
Assume you can read basic code in at least one language, have used an AI assistant, and can run a small program locally. This chapter focuses on Python, but the method applies to other languages.
Start With a Specific Question
A vague request such as “Explain this code” gives the assistant too many possible goals. First name the part you do not understand and the kind of answer you want.
Useful goals include:
- Summarize what the code does.
- Walk through one input step by step.
- Explain why a library feature is used.
- Identify assumptions or possible side effects.
- Compare the code with a simpler implementation.
For example, this function uses a decorator. A decorator is a feature that changes or wraps a function.
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)Instead of asking for a general explanation, ask why the decorator is present and request a walkthrough of fib(5).
Give the Assistant the Context It Needs
Include the smallest set of facts that affects the explanation:
- Your programming language and version, if relevant.
- Where the code runs, such as a web service or data-processing script.
- What the function receives and returns.
- The names and behavior of nearby functions that matter.
- The exact part that seems confusing.
Context prevents the assistant from inventing a purpose for the code. Do not paste an entire repository when one function and its caller are enough.
Keep Secrets and Sensitive Code Out of the Prompt
Remove passwords, access tokens, private keys, customer data, and personal information before sharing code. Replace them with clear placeholders such as DATABASE_URL or CUSTOMER_ID.
Check strings, comments, configuration files, logs, and error messages. Secrets often appear outside the function you intended to share. If the code is proprietary, follow your organization’s approved assistant and data-handling rules.
Write a Clear Explanation Prompt
Combine the goal, context, code, and requested format. For example:
I am reading a Python utility in a data-processing project.
I do not know the lru_cache decorator.
Explain what this function does and why the decorator is used.
Walk through fib(5) step by step. Separate facts visible in the code
from assumptions about the runtime.
<code>
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
</code>Exercise
What is missing from the prompt “Explain this function,” and how would you improve it?
Show answer
It lacks a specific goal and relevant context. Add the language, runtime purpose, confusing feature, input to trace, and desired explanation format.