Question 170
A developer must change exactly one unique exception message in src/api/errors.ts, leaving all other code intact. The old string appears once and the replacement is fully specified. Which built-in tool should Claude Code favor?
Explanation
Correct Answer
Edit is suitable for a targeted replacement when the existing text is a unique, verified anchor. It changes the specified content without reconstructing the rest of the file. The agent should confirm the replacement matches once and inspect the diff to ensure no unintended lines changed. This matches the user's localized request and preserves surrounding imports and functions. If the match proves ambiguous, the agent should stop and inspect before using a broader strategy.
Incorrect Answers
A full rewrite would re-create code that does not need modification and can accidentally alter surrounding logic. The existing string is a unique anchor, so a narrow replacement is safer. File-wide regeneration adds risk without value. The edit should preserve all unrequested behavior.
Grep only locates content; it does not apply a replacement. A search can verify uniqueness, but the modification still needs a writing operation. Choosing a search tool as the final edit would leave the requested change undone. A separate change tool must perform the requested modification.
The file contains more than the message, so deletion would destroy unrelated behavior. There is no requirement to replace the full implementation. A precise in-place edit is the appropriate operation for a unique text change. The unaffected functions remain necessary for application behavior.