Claude Code Best Practices
Master Claude Code with proven workflows, optimization strategies, and expert techniques
Claude Code Best Practices
These aren't theoretical suggestions. They're battle-tested workflows from Anthropic engineers who use Claude Code every day. Follow these patterns, and you'll avoid 80% of common frustrations.
The 80/20 Rule
Following "Explore → Plan → Code → Commit" prevents 80% of common issues. Master this workflow first, then add advanced techniques.
Part 1: Environment Customization
CLAUDE.md — Your Secret Weapon
CLAUDE.md files are persistent instructions that Claude reads every time. Think of them as programming Claude for your specific project.
What to Include
# Project: My Awesome App ## Common Commands- `npm run dev` - Start dev server (port 3000)- `npm test` - Run Jest tests- `npm run lint` - ESLint with auto-fix ## Architecture- `/src/components` - React components (TypeScript)- `/src/lib` - Shared utilities- `/src/app` - Next.js App Router pages ## Code StyleIMPORTANT: Always use TypeScript strict mode.YOU MUST: Add JSDoc comments to exported functions.NEVER: Use `any` type—use `unknown` instead. ## Testing- Write tests BEFORE implementation (TDD)- Avoid mocks unless necessary- Test edge cases: empty arrays, null, errorsEmphasis Keywords
Claude pays extra attention to these:
| Keyword | Effect |
|---|---|
| IMPORTANT: | High priority instruction |
| YOU MUST: | Required behavior |
| NEVER: | Prohibited action |
| CRITICAL: | Absolute requirement |
Where to Place CLAUDE.md
| Location | Scope | Use Case |
|---|---|---|
| ~/.claude/CLAUDE.md | Global | Personal preferences |
| ../CLAUDE.md | Parent dir | Monorepo shared config |
| ./CLAUDE.md | Project root | Project-specific rules |
| ./src/CLAUDE.md | Subdirectory | Module-specific overrides |
Generate Automatically
claude/initClaude analyzes your project and creates a tailored CLAUDE.md.
Update Quickly
Use the # shortcut:
# Add that we now use Prisma instead of raw SQLClaude updates CLAUDE.md for you.
Part 2: The Core Workflows
Workflow 1: Explore → Plan → Code → Commit
The most important workflow. Master this first.
Step 1: Explore
Ask Claude to READ first, not write:
Read src/auth/login.ts and src/lib/jwt.ts to understandour current authentication flow.Now read the tests in tests/auth/ to see what's already tested.Step 2: Plan
Request a detailed plan:
I need to add OAuth support. Think through how to implement this.Claude activates extended thinking and provides a structured plan with potential issues.
Thinking modes:
think— Basic extended thinkingthink hard— Deeper analysisthink harder— Very thoroughultrathink— Maximum thinking time
Step 3: Code
Work through the plan systematically:
Looks good! Start with step 1 - add OAuth provider configuration.Done. Now step 2 - the callback handler.Don't skip steps. Let each piece complete before moving on.
Step 4: Commit
This looks great! Create a commit with an appropriate message.Claude analyzes your changes and writes a meaningful commit message.
Workflow 2: Test-Driven Development
Claude excels with TDD because tests give clear targets.
Step 1: Write Tests First
I need a function that parses markdown frontmatter. Here are examples: Input:---title: Hello---# Content Output:{ title: "Hello", content: "# Content" } Write comprehensive test cases. Use real calls, not mocks.Step 2: Verify Tests Fail
Run the tests to confirm they fail (no implementation yet).Step 3: Commit Tests
Commit the tests.Step 4: Implement Iteratively
Now implement the parser. Run tests after each change.Claude implements, runs tests, fixes failures, repeats until green.
Step 5: Commit Implementation
All tests passing! Commit the implementation.Workflow 3: Visual Iteration
For UI work, iterate with screenshots.
The Process
- Share design mock (Figma screenshot, wireframe)
- Claude implements
- Take screenshot of result
- Paste and ask for fixes
- Repeat until perfect
[paste screenshot of Figma design] Implement this dashboard component.[paste screenshot of result] Close but the spacing is off. Cards should have 24px gap, not 16px.[paste updated screenshot] Better! The blue should be #3B82F6 not #60A5FA.Pro tip: On macOS, use Cmd+Ctrl+Shift+4 to screenshot directly to clipboard.
Workflow 4: Codebase Q&A
Use Claude like a knowledgeable colleague.
How does our logging system work?Where should I add a new API endpoint?Show me the pattern I should follow.When did we switch from Express to Next.js? Why?Claude searches your codebase and git history to answer.
Part 3: Tool Permissions & Extensions
Configure Permissions
Three ways to manage what Claude can do:
1. Interactive (during sessions):
Claude: I need permission to run bash commands.You: [Check "Always allow" → Allow]2. Via command:
/permissions3. Manual configuration:
Edit .claude/settings.json:
{ "allowedTools": ["read", "write", "edit", "bash", "grep"], "autoApprove": { "bash": ["npm install", "npm run dev", "git status"] }}Custom Slash Commands (Skills)
Create reusable prompts in .claude/commands/:
.claude/commands/fix-issue.md
# Fix GitHub Issue Pull issue $ARGUMENTS from GitHub and:1. Read the issue description2. Locate relevant code3. Propose a fix4. Implement with tests5. Create PR referencing issueUsage:
/fix-issue 1234See Claude Skills Guide for comprehensive coverage.
Part 4: Optimization Strategies
Be Specific
Use Images
Claude can see images. Use this for:
- UI/UX work — Design mocks, screenshots
- Bug reports — Show what's broken
- Data visualization — Chart outputs
- Architecture — Diagrams, ERDs
Reference Files Precisely
Use tab-completion:
Look at sr[TAB] → src/au[TAB] → src/auth/login.tsPrecise references = faster, more accurate responses.
Clear Context Between Tasks
/clearUse liberally when:
- Switching between unrelated tasks
- After completing major features
- Context feels muddled
- Starting something complex
Course Correct Early
Request plans before coding:
Think through how to implement real-time notifications.Interrupt with Escape:
Press Escape to stop Claude mid-response.
Double-tap Escape: Edit your last prompt.
Ask for undos:
Undo those changes. I want to try a different approach.Part 5: Multi-Claude Workflows
Parallel Verification
Use multiple Claude instances for better quality:
Terminal 1 (Claude A):
Implement the user authentication systemTerminal 2 (Claude B):
Review the authentication code in src/auth/ for security issuesTerminal 1 (Claude A):
Fix these security issues: [paste Claude B's findings]Multiple Repository Checkouts
Work on multiple features simultaneously:
# Clone to multiple directoriesgit clone git@github.com:you/project.git project-feature-agit clone git@github.com:you/project.git project-feature-b # Or use git worktrees (lighter)git worktree add ../project-feature-a feature-agit worktree add ../project-feature-b feature-bRun Claude in each directory with separate tasks.
Headless Operations
For batch operations:
# Loop through filesfor file in src/**/*.ts; do claude -p "Review $file for security issues" >> report.txtdonePart 6: Pro Tips
Hidden Shortcuts
| Shortcut | Action |
|---|---|
| Cmd/Ctrl + K | Clear screen |
| Escape | Interrupt Claude |
| Escape Escape | Edit last prompt |
| # | Update documentation |
| / | Slash commands menu |
| Tab | File/folder completion |
Magic Phrases
| Phrase | Effect |
|---|---|
| think / think hard | Extended thinking |
| IMPORTANT: | Emphasis in instructions |
| /init | Generate CLAUDE.md |
| /clear | Reset context |
Debugging Claude
Claude is stuck:
/clearLet's start over. [Rephrase clearly]Claude is hallucinating:
Stop. Read the actual code first. Don't assume.Same mistake repeatedly:
IMPORTANT: Do NOT use the `any` type.YOU MUST use proper TypeScript types.Results not good enough:
think harder This solution won't scale. Consider:1. What happens with 1M users?2. Database indexing3. Caching layer4. Rate limitingKey Takeaways
The Golden Rules
- Explore before coding — Read files, understand context
- Plan before implementing — Use
thinkmodes - Be specific — Vague prompts = poor results
- Iterate with feedback — Visual/test feedback improves outcomes
- Clear context regularly — Fresh start = focused results
- Use TDD — Tests guide implementation
- Leverage tools — Skills, MCP, custom commands
- Multiple Claudes for complex tasks — Separate contexts catch issues
Quick Reference
Essential Commands:
/init— Generate CLAUDE.md/clear— Reset context/permissions— Configure tools#— Update documentation
Workflows:
- Explore → Plan → Code → Commit
- TDD: Tests → Red → Green → Commit
- Visual: Mock → Implement → Screenshot → Iterate
Practice Exercises
Exercise 1: Create Your CLAUDE.md
cd your-projectclaude/init # Edit to add:# - Common commands# - Code style rules# - Testing guidelinesExercise 2: TDD Feature
- Write tests for a new feature
- Verify tests fail
- Have Claude implement until green
- Commit tests, then implementation
Exercise 3: Visual Iteration
- Find a UI design
- Share screenshot with Claude
- Implement and compare
- Iterate 3-4 times until perfect
Next Steps
- Claude Skills Guide — Create custom commands
- MCP and Cursor — Extend capabilities
- Automation Track — Automate workflows
You're Now a Power User!
These practices come from thousands of hours of real-world usage. Apply them consistently, and you'll see dramatic improvements in productivity and code quality. Start with the Explore → Plan → Code → Commit workflow—master that, then add other techniques.