Sub topic 5
Sharing a selection
This chapter assumes you have used an AI coding assistant before, can read basic Python, and know how to select lines in an editor or pass a line range in a terminal. It builds on the previous chapter, which covered sharing whole files.
What a selection is
A selection is a specific range of lines or a block of code that you point the assistant at. It can be a function, a loop, a class, or a few lines inside one. The selection tells the assistant where to look. It also tells the assistant where to change things.
A selection is not a replacement for the file. In most tools it works best as a pointer inside a file that is also available. The file gives the surroundings. The selection gives the focus.
When a selection beats a whole file
Use a selection when the task is local. Use a whole file when the task depends on how the parts fit together.
| Situation | Better choice | Reason |
|---|---|---|
| Fix one function that raises an error | Selection | The problem is in a small, known region |
| Explain what a dense block does | Selection | Surrounding code adds noise |
| Rename a helper used across the module | Whole file | The assistant must find every use |
| Reorganize how three functions call each other | Whole file | The structure is the subject |
| Review a 600-line file for style problems | Whole file | The task covers all of it |
A large file spends part of your context budget on code that has nothing to do with the task. It can also pull the assistant toward editing things you did not mean to touch. A selection narrows both.
A worked example
Here is the small file used throughout this tutorial.
def average(values):
return sum(values) / len(values)
print(average([]))Running it fails with this error.
Traceback (most recent call last):
File "stats.py", line 4, in <module>
print(average([]))
File "stats.py", line 2, in average
return sum(values) / len(values)
ZeroDivisionError: division by zeroThe traceback points to line 2, inside average. The cause is in lines 1-2, so those lines are the right selection. Line 4 only triggers the problem.
A weak request leaves the assistant to guess:
Fix my code.A request that uses a selection removes the guessing:
Context: stats.py (attached), selection: the average function (lines 1-2).
Error: ZeroDivisionError: division by zero when average([]) is called.
Task: Change average so an empty list returns None instead of raising. Keep the function signature and do not change other code.This request names the file, marks the selection, supplies the error, and states the task. The last sentence limits the edit to the selected function.
How to choose the boundaries
Select a complete unit. A function, a method, or a class body is usually a good boundary. A selection that starts in the middle of an if block or cuts a function in half forces the assistant to guess what the missing lines do.
Check these points before you send:
- Does the selection include the line the error points to?
- Does it include the definitions the lines use, such as a constant or a type, or can the assistant find them in the attached file?
- Is the selection free of unrelated code, such as the neighboring function?
- Do the line numbers you cite match the current version of the file?
The last point matters because line numbers drift. If you add or remove lines after copying a range, the numbers in your request point at the wrong code. Name the function as well as the lines, as in the example above, so the pointer survives small edits.
Common errors
- Selecting too little. If you share only
return sum(values) / len(values), the assistant cannot see the function name, the parameter, or the callers. It may invent a signature. - Selecting too much. Pasting a whole module as a "selection" removes the benefit. The assistant has no signal about which part matters.
- Dropping the file. A selection pasted alone loses imports, type definitions, and neighboring helpers. Attach the file, or mention what the selection depends on.
- Leaving the task out. A selection says where to work. It does not say what to do. Always add the task.
- Stale selection. If you changed the code since you selected it, re-select it. The assistant works from what you sent, not from your current editor.
Exercise
A function parse_date in a 400-line file utils.py returns the wrong month for dates in January. The bug is in lines 52-60. Write a short request that uses a selection. Say what you would include besides the selection.
Show answer
Attach utils.py, mark the selection as the parsedate function (lines 52-60), and include a concrete example of the failure, such as "parsedate('2026-01-15') returns month 12; expected 1". State the task: "Fix the month calculation in parse_date. Change only the selected function." Naming the function as well as the lines keeps the pointer accurate if line numbers shift.