Code Generation & Feature Building
After this lesson, you will be able to:
- Use Claude Code to build complete features just by describing what you want in plain English
- Apply the vertical slice workflow: backend + frontend + tests all built together in one pass
- Generate API endpoints, React components, and database migrations with a single prompt
- Write prompts that ask for proper types, error handling, and tests, then verify the result by running them
Before you start
#The Vertical Slice Workflow
This is where Claude Code really shines. Building a complete feature (backend, frontend, tests) in a single session is one of the most satisfying experiences in modern development. Once you do it once, you will never go back to the old layer-by-layer approach.
You ask Claude Code: 'Add user authentication.' What does it do first?
#Example: Building a Feature End-to-End
Here is a real prompt that builds a complete feature:
Build a "bookmark lesson" feature:
Backend:
- POST /api/bookmarks with { lessonSlug, trackSlug } body
- GET /api/bookmarks to list all bookmarked lessons
- DELETE /api/bookmarks/:id to remove a bookmark
- Use the existing database pattern in src/lib/db.ts
Frontend:
- Add a bookmark icon button to the LessonHeader component
- Toggle state: filled star = bookmarked, outline star = not bookmarked
- On click, call the API to add/remove the bookmark
- Show bookmarked lessons on the /dashboard page
Tests:
- API tests for all three endpoints
- Component test for the bookmark toggle behavior
Follow the patterns in existing API routes and components.
A prompt like this asks Claude Code to touch several files across the stack, often around five to ten for a small feature, and to write tests for them. Run the tests yourself before you trust it.
Try it! Pick a small feature for a project you are working on (even a personal one). Write a prompt like the example above: describe the backend endpoints, the frontend component, and the tests you want. Paste it into Claude Code and watch it build everything end-to-end. Your first vertical slice will be an eye-opener.
#Generating API Endpoints
Tests: Verify each template includes method, route, validation, error handling, and pattern reference.
#Building React Components
When asking Claude Code to build components, specify:
- Where the component lives in the file hierarchy
- Props interface
- Styling approach (Tailwind classes, CSS modules, etc.)
- State management pattern
- An existing component to use as a reference
#Example: Component Generation Prompt
Create a LessonCard component in src/components/content/lesson-card.tsx:
Props:
- title: string
- description: string
- trackSlug: string
- lessonSlug: string
- estimatedMinutes: number
- difficulty: "beginner" | "intermediate" | "advanced"
- isCompleted: boolean
Behavior:
- Clicking the card navigates to /tracks/{trackSlug}/{lessonSlug}
- Show a colored difficulty badge (green/yellow/red)
- Show a checkmark icon when isCompleted is true
- Show estimated reading time with a clock icon
Style:
- Use Tailwind CSS and the cn() utility from @/lib/cn
- Follow the card pattern in src/components/ui/Card.tsx
- Use the glass utility class for the card background
- Hover effect: subtle scale and glow
Export as a named export, not default.
You ask Claude Code to 'create a new React component.' Without any other context, what is the most likely problem with the generated code?
Without specificity, Claude Code has to guess: Should it use CSS modules or Tailwind? Default or named export? PascalCase or camelCase file name? Where in the file tree? What props interface? Each guess is a potential mismatch with your project's conventions. The more you specify, the better the result.
#Writing Tests Alongside Code
The most effective pattern is asking Claude Code to write tests alongside the code:
Add input validation to the ContactForm:
1. Add zod validation for email (valid format) and message (min 10 chars)
2. Show inline error messages below each field
3. Write vitest tests verifying:
- Valid submission works
- Invalid email shows error
- Short message shows error
- Error clears when user fixes the input
This produces tested code from the start, not code that you have to add tests to later.
#Real Example: Building a Complete Feature
Here is what a full vertical-slice session looks like:
Prompt 1 (Plan)
Plan how to add a "lesson progress" feature:
- Users can mark lessons as complete
- Progress is stored in localStorage via Zustand
- Each track shows a progress bar
- The dashboard shows overall completion percentage
Don't write code yet -- just outline the plan.
Prompt 2 (Execute backend/store)
Execute step 1: Create the progress store.
Add a Zustand store at src/lib/stores/progress-store.ts with:
- markComplete(trackSlug, lessonSlug)
- isComplete(trackSlug, lessonSlug) -> boolean
- getTrackProgress(trackSlug) -> { completed: number, total: number }
- getOverallProgress() -> number (0-100)
Persist to localStorage. Follow the pattern in ui-store.ts.
Write tests.
Prompt 3 (Execute frontend)
Execute step 2: Add progress UI.
- Add a "Mark Complete" button to the lesson page footer
- Add a progress bar to each TrackCard on the tracks listing page
- Add an overall progress ring to the dashboard
Use the progress store we just created. Follow existing component patterns.
Prompt 4 (Verify)
Run the tests and fix any failures. Then run the build to check for type errors.
#Key Takeaways
- Vertical slices produce better code: building backend, frontend, and tests together in one pass ensures integration from the start and catches mismatches early
- Reference existing patterns in every prompt: "follow the pattern in [file]" is the most powerful phrase in Claude Code prompting; it anchors the generated code to your project's conventions
- Include tests in the generation prompt: asking for tests alongside code forces better implementations and produces tested features from the start
- One feature per session, with plan-then-execute: break complex features into vertical slices, plan first, then execute in phases that each produce working, testable increments
#Quick Check
What is a 'vertical slice' in the context of Claude Code feature building?
You want a 'complete user authentication system' (signup, login, password reset, sessions, tests). Pick the workflow most likely to produce reviewable code.