Adding Features to Existing Code

Extending a working project is the most common real-world AI coding task. This lesson gives you 20 proven prompts and the patterns to use them safely.

Intermediate 12 min read Lesson 12 of 20

The Challenge of Existing Code

Adding to new code is easy - the AI knows everything because it just wrote it. Adding to existing code is harder because the AI does not know your codebase unless you show it. The golden rule:

Before asking the AI to add a feature, show it the code it must integrate with.

Context First

Start every feature-add session by giving the AI the relevant context:

Context-first prompt pattern
# WRONG - no context (AI invents an incompatible solution):
> Add a password reset feature

# CORRECT - context first:
> I need to add a password reset feature. Here is the relevant context:

  Current UserModel (relevant methods):
  [paste findByEmail(), create(), updatePassword()]

  Current AuthController (relevant methods):
  [paste showLogin(), processLogin()]

  Database: users table has columns: id, email, password_hash, name

  Now add password reset:
  - "Forgot Password" link on login page
  - Form to enter email address
  - Generate a secure token, save to password_resets table (email, token, expires_at)
  - Send email with reset link (use PHP mail())
  - Reset form where user enters new password
  - Invalidate token after use

20 Feature-Add Prompt Templates

Copy and adapt any of these for your project. Each one includes the key context needed.

Feature 1: Email notifications
> Add email notifications to the existing TaskFlow app.
  Read UserModel and TaskModel first.

  When a task is assigned to a user:
  - Send email to the assigned user
  - Subject: "New task assigned: [task title]"
  - Body: task title, description, due date, project name, link to task

  Create: app/services/EmailService.php using PHP mail()
  Update: TaskModel::create() and update() to call EmailService
  Create: config/email.php with FROM_EMAIL, FROM_NAME constants

  Do NOT change any existing method signatures.
Feature 2: File attachments
> Add file attachments to tasks.
  Read tasks table schema and views/tasks/form.php first.

  - Users can upload up to 5 files per task (PDF, images, docs)
  - Max 10MB per file
  - Store in /storage/attachments/{task_id}/
  - Table: task_attachments (id, task_id, filename, original_name, size, mime_type, uploaded_by, created_at)
  - Show attachment list in task edit form with delete button
  - Download link for each attachment

  Add to views/tasks/form.php (existing form).
  Create app/models/AttachmentModel.php for DB operations.
Feature 3: Search
> Add a global search feature to the app.
  Read the header.php template and UserModel/TaskModel/ProjectModel first.

  - Search box in the navbar (all pages)
  - Searches: projects (name, description), tasks (title, description), users (name, email - admin only)
  - Results page at /search.php?q=query
  - Group results by type with count: "3 Projects, 12 Tasks"
  - Highlight the search term in results (bold)
  - Min 2 characters to search
Feature 4: Export to CSV
> Add CSV export for tasks.
  Read TaskModel::getByProject() first.

  Add "Export CSV" button to views/projects/show.php.
  Clicking it calls GET /api/tasks/export?project_id=X
  which returns a CSV file download with columns:
  Task ID, Title, Status, Priority, Assigned To, Due Date, Created At, Completed At

  Implement in api/tasks.php alongside existing endpoints.
  No separate library needed - use fputcsv().
Feature 5: Activity log
> Add an activity log to track all changes.
  Read TaskModel and ProjectModel first.

  Table: activity_log (id, user_id, action_type, entity_type, entity_id, old_value, new_value, created_at)

  Log these events:
  - task.created, task.updated, task.status_changed, task.deleted
  - project.created, project.archived, project.member_added

  Create: app/services/ActivityLogger.php
  Update: TaskModel and ProjectModel to call ActivityLogger after each operation.

  Show activity log at bottom of views/projects/show.php
  (last 20 events, most recent first).
More feature templates - quick list
# Feature 6: Dark mode
"Add a dark mode toggle to the navbar. Store preference in localStorage.
 Toggle a .dark-mode class on body. Write CSS for dark mode in assets/css/app.css
 (dark backgrounds, light text for all components)."

# Feature 7: Two-factor authentication
"Add TOTP-based 2FA to the login flow. Use a QR code library.
 Read AuthController::processLogin() first.
 Users can enable 2FA in their profile settings."

# Feature 8: Recurring tasks
"Add recurring tasks. Read TaskModel::create() first.
 Add recurrence fields to tasks: recurrence_type(daily/weekly/monthly/none), recurrence_end.
 Create a cron-style PHP script that generates the next occurrence when a task is completed."

# Feature 9: Notifications bell
"Add an in-app notification center. Badge on navbar bell icon showing unread count.
 Table: notifications (id, user_id, type, message, entity_type, entity_id, read_at, created_at).
 Dropdown shows last 10 notifications. Mark as read on click."

# Feature 10: Subtasks
"Add subtasks (checklist items) to tasks. Read tasks table schema.
 Table: subtasks (id, task_id, title, completed, sort_order).
 Show as checklist inside task detail. Progress bar showing completion %.
 Task progress bar updates as subtasks are checked."

Safe Extension Patterns

Safety rules for feature adds
# Rule 1: New files are safer than modified files
"Create a new EmailService class instead of adding email logic to UserModel"
- New files cannot break existing functionality
- Modified files can introduce bugs in untouched methods

# Rule 2: State what must NOT change
"Add the new method WITHOUT modifying create() or update()"
"Only add to the views, do not touch any PHP files"

# Rule 3: Add feature flags during development
"Wrap the new notification code in: if (FEATURE_NOTIFICATIONS)"
"Add FEATURE_NOTIFICATIONS = false to config/app.php"
- Test the feature flag disabled (everything still works)
- Then enable and test the feature

# Rule 4: Add to a branch, not main
"Before starting: remind me to create a git branch: git checkout -b feature/email-notifications"

Testing After Adding

Post-feature verification prompts
# Verify nothing broke:
> Run all existing tests. Did any fail after the changes?
> Review what files were changed. Is anything modified that I did not intend?

# Check the new feature:
> Walk me through the email notification feature you just added.
  What happens step by step when a task is assigned?

# Check for edge cases:
> What happens if the email server is unreachable when a task is assigned?
  Does it fail silently or throw an error that breaks the task save?

# Security check:
> Review the file attachment upload code for security issues.
  Can a user upload PHP files? Can they access other users' files?

Frequently Asked Questions

This is a common issue. Prevent it by being explicit: "Add this feature WITHOUT changing any existing methods. Only add new methods or new files." Review the diff carefully - in Claude Code you can see exactly what changed. If the AI modified something unexpectedly, ask it to revert those changes: "Revert all changes to UserModel.php except the new sendWelcomeEmail() method."

Decompose it. "Add email notifications" might touch: UserModel (send method), a new EmailService, config (SMTP settings), and multiple controller files. Do one component per session: start with EmailService (new file, clean), then update UserModel to use it, then update controllers. Smaller changes are easier to verify and less risky.

Yes, but you need to give it the relevant context. Show it the file(s) that will change and the interface it must integrate with. "Read UserModel.php. Now add a method sendPasswordResetEmail(string $email) that..." The AI sees the existing class, understands its patterns, and extends it consistently.