Project Structure Optimization
AI-optimized project organization: read less, understand faster
Core Concept: Keep Files Small
AI has an important limitation when reading code: limited context window. When AI reads a file, the larger the file and the more irrelevant information it contains, the less efficient AI becomes at understanding core logic.
Large Files vs Small Files
| Approach | AI Reading Experience | Problem |
|---|---|---|
| ๐ Large File (1000+ lines) | AI needs to read lots of irrelevant code to find the target | Context pollution, low comprehension efficiency |
| ๐ Small Files (100-300 lines) | AI can precisely read the needed module | Clear context, precise modifications |
Principle: One file does one thing, keep it within 100-300 lines.
Recommended Directory Structure
Frontend Projects (React/Next.js)
src/
โโโ app/ # Page routes
โ โโโ (auth)/ # Route groups
โ โ โโโ login/
โ โ โโโ register/
โ โโโ dashboard/
โโโ components/ # UI components
โ โโโ ui/ # Base components (Button, Input...)
โ โโโ features/ # Business components
โ โโโ UserCard/
โ โ โโโ index.tsx # Component entry
โ โ โโโ UserCard.tsx # Main component
โ โ โโโ Avatar.tsx # Sub-component
โ โ โโโ types.ts # Type definitions
โ โโโ OrderList/
โโโ hooks/ # Custom Hooks
โโโ lib/ # Utility functions
โโโ types/ # Global type definitions
โโโ docs/ # ๐ Project documentation
โ โโโ architecture.md # Architecture overview
โ โโโ api-contracts.md # API contracts
โ โโโ decisions/ # Architecture Decision Records
โโโ prompts/ # ๐ค AI Prompts management
โโโ features/ # Feature development prompts
โโโ templates/ # General templatesBackend Projects (Node.js/NestJS)
src/
โโโ modules/ # Business modules
โ โโโ user/
โ โ โโโ user.controller.ts
โ โ โโโ user.service.ts
โ โ โโโ user.repository.ts
โ โ โโโ dto/ # Data Transfer Objects
โ โ โ โโโ create-user.dto.ts
โ โ โ โโโ update-user.dto.ts
โ โ โโโ entities/
โ โ โโโ user.entity.ts
โ โโโ order/
โโโ common/ # Common modules
โ โโโ decorators/
โ โโโ filters/
โ โโโ guards/
โ โโโ interceptors/
โโโ config/ # Configuration management
โโโ docs/ # ๐ Project documentation
โโโ prompts/ # ๐ค AI Prompts management๐ Managing Project Documentation
Maintaining clear documentation in your project helps AI quickly understand project context:
Recommended Documentation Structure
docs/
โโโ README.md # Project overview (AI reads first)
โโโ architecture.md # System architecture
โโโ tech-stack.md # Technology stack
โโโ api-contracts/ # API contract documents
โ โโโ user-api.md
โ โโโ order-api.md
โโโ database/ # Database related
โ โโโ schema.prisma # Or erd.md
โ โโโ migrations.md
โโโ decisions/ # Architecture Decision Records (ADR)
โโโ 001-use-nextjs.md
โโโ 002-auth-strategy.mdDocumentation Writing Principles
| Principle | Description |
|---|---|
| Concise and Clear | AI doesnโt need detailed usage instructions, just needs to know โwhatโ and โwhyโ |
| Structured | Use tables, lists, code blocks for easy AI parsing |
| Keep Updated | Outdated documentation is worse than no documentation |
๐ค Managing Prompts
Manage commonly used AI Prompts as project assets for team reuse and iteration:
Recommended Prompts Structure
prompts/
โโโ README.md # Prompts usage guide
โโโ features/ # Feature development related
โ โโโ new-api-endpoint.md
โ โโโ new-component.md
โ โโโ add-database-table.md
โโโ refactoring/ # Refactoring related
โ โโโ extract-hook.md
โ โโโ split-component.md
โโโ templates/ # General templates
โโโ code-review.md
โโโ bug-fix.mdPrompt File Example
# Add New API Endpoint
## Use Case
Use when adding a new REST API endpoint to a module
## Prompt Template
Please add a new API endpoint under `src/modules/{module}`:
1. Add route handler in controller
2. Add business logic in service
3. Create corresponding DTO files
4. Add unit tests
Requirements:
- Follow existing code style
- Add Swagger documentation annotations
- Handle common error cases๐ For detailed content on Prompts management, see Chapter 4: Workflow.
File Splitting Practices
Splitting Components
// โ Bad: All logic in one large file
// components/UserDashboard.tsx (500+ lines)
// โ
Good: Split into multiple small files
// components/UserDashboard/
// โโโ index.tsx # Export entry
// โโโ UserDashboard.tsx # Main component (~100 lines)
// โโโ UserStats.tsx # Stats card (~80 lines)
// โโโ RecentActivity.tsx # Recent activity (~100 lines)
// โโโ hooks.ts # Component-specific hooks
// โโโ types.ts # Type definitionsSplitting Services
// โ Bad: One giant service
// services/user.service.ts (800+ lines)
// โ
Good: Split by responsibility
// services/user/
// โโโ index.ts # Export entry
// โโโ user-auth.service.ts # Auth related (~150 lines)
// โโโ user-profile.service.ts # Profile management (~120 lines)
// โโโ user-settings.service.ts # Settings management (~100 lines)Key Principles Summary
| Principle | Practice |
|---|---|
| Single Responsibility | Each file handles only one functional module |
| File Size | Keep within 100-300 lines |
| Clear Naming | File name directly reflects its content |
| Proximity Principle | Keep related files in the same directory |
| Docs Alongside Code | Version control project docs with code |
Next Steps
Next, weโll explore Database Schema Management to learn how to help AI efficiently understand your data models.
Last updated on: