← Lessons/Smart File Editing

Learning Objectives

  • ◆Edit existing files by describing the changes you want
  • ◆Reference specific files and functions in your prompts
  • ◆Apply prompting best practices for accurate, consistent results

Modifying Existing Code

One of Claude Code's most powerful features is editing existing files. You don't need to specify file paths — just describe the change you want, and Claude will find the right files and make targeted modifications.

Claude understands your entire codebase, so it makes changes that are consistent with your existing patterns and conventions.

For more precision, you can reference specific files: "In lib/api.ts, add error handling to the fetchUsers function." This helps Claude zero in on exactly what you want changed.

lesson-03 — Modifying Existing Code
claude > Add error handling to the fetchUsers
function in the dashboard
 
I found `fetchUsers` in lib/api.ts. I'll add
try/catch with proper error types.
 
┌─ lib/api.ts ────────────────────────────┐
│ - const res = await fetch('/api/use.. │
│ - return res.json() │
│ + try { │
│ + const res = await fetch('/api/u.. │
│ + if (!res.ok) throw new ApiError.. │
│ + return res.json() │
│ + } catch (err) { │
│ + console.error('Failed:', err) │
│ + throw err │
│ + } │
└─────────────────────────────────────────┘