π Code Organization β
Structuring codebases for navigability and maintainability.
Organization strategies β
By feature (recommended) β
src/
βββ features/
β βββ auth/
β βββ billing/
β βββ users/
βββ shared/
βββ lib/Everything related to a feature lives together.
By layer (traditional) β
src/
βββ controllers/
βββ services/
βββ repositories/
βββ models/
βββ utils/Grouped by technical role. Gets unwieldy at scale.
By domain (DDD-inspired) β
src/
βββ domains/
β βββ identity/
β βββ catalog/
β βββ checkout/
βββ infrastructure/
βββ application/Naming conventions β
| Type | Convention | Example |
|---|---|---|
| Files | kebab-case | user-service.ts |
| Classes | PascalCase | UserService |
| Functions | camelCase | getUserById |
| Constants | UPPER_SNAKE | MAX_RETRY_COUNT |
| Types/Interfaces | PascalCase | UserProfile |
| Directories | kebab-case | user-management/ |
Module boundaries β
- Each module/feature exposes a public API (index.ts barrel file)
- Internal implementation is private
- No circular dependencies between modules
- Shared code goes in
shared/orlib/
File size guidelines β
- Keep files under 300 lines
- If a file has more than 5 exports, consider splitting
- One class/component per file
- Group related constants in a dedicated file
Import order β
typescript
// 1. External packages
import express from 'express'
import { z } from 'zod'
// 2. Internal shared modules
import { logger } from '@/shared/logger'
// 3. Relative imports (same feature)
import { UserRepository } from './user.repository'