Claude Code: Claude Code Basic Usage
Last updated: 2026-08-31
Basic usage is Claude Code's daily toolkit — from writing code to fixing bugs, from refactoring to writing tests, covering developers' core work scenarios.
💡 Tip: Claude Code's greatest strength is not single-shot answers, but the "understand project → modify code → verify results → iterate fixes" closed-loop capability.
📋 Prerequisites: Chapter 7 - Interactive Mode and Operation Guide
1. What You'll Learn
- Code generation and completion
- Bug fixing and debugging
- Code refactoring
- Test writing
- Code review
- Documentation generation
2. Code Generation
(1) Create Files from Scratch
TEXT
📖 Display only
> Create src/services/email.ts with email sending service:
> - Support HTML and plain text formats
> - Use template engine for rendering
> - Add retry mechanism (max 3 attempts)
> - Write unit tests (mock SMTP)
Claude Code:
→ Creating src/services/email.ts
→ Creating src/services/__tests__/email.test.ts
→ Installing dependencies: nodemailer, handlebars
→ Running: npm test -- email.test.ts
✅ 5 tests passed
(2) Extend Based on Existing Code
TEXT
📖 Display only
> Add password reset functionality to UserService
Claude Code:
→ Reading src/services/user.ts
→ Reading src/models/user.ts
→ Reading src/services/email.ts (existing email service)
→ Modifying src/services/user.ts (adding resetPassword)
→ Modifying src/routes/user.ts (adding POST /reset-password)
→ Creating tests for password reset
✅ All tests passed
▶ Example 1: Generate CRUD Module
TEXT
📖 Display only
> Create complete Product CRUD module:
> 1. Prisma model definition
> 2. Repository layer
> 3. Service layer
> 4. Controller layer
> 5. Route definitions
> 6. Complete tests
Claude Code: Created 6 files, 12 tests all passing ✅
3. Bug Fixing
(1) Specify Bug Fix
TEXT
📖 Display only
> Fix the relativeTime calculation error in src/utils/date.ts when crossing years
Claude Code:
→ Reading src/utils/date.ts
→ Found bug: relativeTime uses daysOnly comparison
→ Fixing: adding year boundary check
→ Running: npm test -- date.test.ts
✅ All tests passed (including new edge case)
(2) Fix Based on Error Messages
TEXT
📖 Display only
> Run tests and fix all failing cases
Claude Code:
→ Running: npm test
❌ 3 tests failed
→ Reading failed test files...
→ Fixing each failing test
→ Running: npm test
✅ All 47 tests passed
▶ Example 2: Runtime Error Fix
TEXT
📖 Display only
> App startup error: TypeError: Cannot read property 'id' of undefined at UserController.getProfile
Claude Code:
→ Found: getProfile doesn't check if req.user exists
→ Adding null check with proper error response
→ Running: npm test -- user.controller.test.ts
✅ All tests passed
4. Code Refactoring
(1) Function-Level Refactoring
TEXT
📖 Display only
> Refactor formatCurrency in src/utils/format.ts to use Intl.NumberFormat
Claude Code:
→ Replacing manual formatting with Intl.NumberFormat
→ Adding locale support (default: en-US)
→ Running: npm test -- format.test.ts
✅ All tests passed
(2) Module-Level Refactoring
TEXT
📖 Display only
> Reorganize all utility functions under src/helpers/ into src/utils/ by function:
> - Date related → utils/date.ts
> - String related → utils/string.ts
> - Number related → utils/number.ts
> Update all import paths
Claude Code:
→ Moving functions with proper exports
→ Updating 32 import statements
→ Deleting src/helpers/
→ Running: npm test
✅ All 89 tests passed
5. Test Writing
▶ Example 3: Batch Test Generation
TEXT
📖 Display only
> Write tests for all services under src/services/ without tests, coverage target 80%
Claude Code:
→ Scanning 8 service files
→ Missing tests found in 3 services
→ Writing tests for each
→ Running: npm test
✅ All new tests passed
Coverage average: 85% ✓
6. Code Review and Documentation
(1) Code Review
TEXT
📖 Display only
> Review src/auth/jwt.ts for security issues
Claude Code:
⚠️ Found 3 issues:
1. Hardcoded secret key (line 12) → Use env variable
2. No token expiry validation (line 34) → Add expiry check
3. Algorithm not specified (line 8) → Force RS256
(2) Documentation Generation
TEXT
📖 Display only
> Generate API documentation for all routes under src/api/
Claude Code:
→ Reading all route files (12 files)
→ Generating docs/api.md with endpoint listing, examples, and error codes
7. Comprehensive Example: Full-Flow Development
TEXT
📖 Display only
> Implement user permission system:
> 1. Define Role and Permission models
> 2. Implement RBAC middleware
> 3. Add permission check decorators
> 4. Write complete integration tests
> 5. Generate API documentation
Claude Code: 8 files created, 4 modified, 15 tests passing ✅
❓ FAQ
Q How's the quality of Claude Code's generated code?
A High for common patterns and standard implementations. Complex business logic and special cases still need human review. Always review generated code.
Q Can it fix multiple bugs at once?
A Yes. Describe each bug clearly and Claude Code fixes them one by one. But focusing on one bug at a time reduces error probability.
Q Will refactoring break existing functionality?
A If the project has comprehensive tests, Claude Code ensures they pass. Without tests, refactoring is riskier — recommend adding tests first.
Q Are the tests well-written?
A Unit test quality is good, especially for happy paths and common edge cases. Boundary conditions and complex mock scenarios may need manual supplementing.
Q Can code review replace human review?
A Not entirely. Claude Code excels at finding common security issues and style problems, but business logic correctness still requires human judgment.
Q How to make Claude Code follow project code style?
A Write coding conventions in CLAUDE.md. Also have it read ESLint/Prettier config first.
📖 Summary
- Code generation: From single files to complete CRUD modules
- Bug fixing: Auto-locate and fix based on error messages or descriptions
- Refactoring: Function to module level, auto-update all references
- Testing: Batch supplement tests, ensure coverage targets met
- Review & docs: Security review, API documentation auto-generation
- Core advantage: Understand → Modify → Verify → Iterate closed loop
📝 Exercises
- Basic (⭐): Use Claude Code to generate unit tests for a function, check test quality.
- Intermediate (⭐⭐): Intentionally introduce a bug, use Claude Code to fix it, observe its locating and fixing process.
- Advanced (⭐⭐⭐): Have Claude Code complete a full feature module (model+service+route+test+docs), review all generated code.