Skip to content

Explaining Code

LLMs excel at reading code and explaining what it does in plain language. This is useful for understanding unfamiliar codebases or learning new languages.

Prompt
Explain what this Python code does, line by line:

def fib(n):
    a, b = 0, 1
    for _ in range(n):
        a, b = b, a + b
    return a

The model walks through each line, explaining both the mechanics and the intent.

Response
This function computes the nth Fibonacci number:

1. `def fib(n):` — defines a function taking n (which
   Fibonacci number to compute).

2. `a, b = 0, 1` — initializes two variables: a=0 and
   b=1, the first two Fibonacci numbers.

3. `for _ in range(n):` — loops n times. The _ means
   we don't need the loop variable.

4. `a, b = b, a + b` — simultaneously updates a to the
   old b, and b to the sum of old a + old b. This
   shifts the window forward by one position.

5. `return a` — returns the nth Fibonacci number.

Example: fib(6) returns 8 (sequence: 0,1,1,2,3,5,8).

You can adjust the explanation level — ask for a beginner-friendly version or a deep technical analysis.

Prompt
Explain this SQL query to someone who has never used SQL:

SELECT department, AVG(salary) as avg_salary
FROM employees
WHERE hire_date > '2020-01-01'
GROUP BY department
HAVING AVG(salary) > 75000
ORDER BY avg_salary DESC;

The model adapts its language for non-technical readers, using analogies and plain terms.

Response
Think of this as asking a question about a spreadsheet
of employee data:

1. Look at all employees hired after January 1, 2020
2. Group them by their department
3. Calculate the average salary for each department
4. Only keep departments where that average is above
   $75,000
5. Sort the results from highest to lowest average salary

The result is a list of well-paying departments for
recent hires.

Key takeaway: LLMs can explain code at any level of detail. Specify your audience ("explain to a beginner", "give a senior engineer review") for the best results.

Tips:
- "Explain line by line" for detailed walkthrough
- "What's the time complexity?" for analysis
- "What could go wrong?" for potential bugs
- "How would you improve this?" for refactoring ideas