Skip to content

πŸ“ Code Organization ​

Structuring codebases for navigability and maintainability.

Organization strategies ​

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 ​

TypeConventionExample
Fileskebab-caseuser-service.ts
ClassesPascalCaseUserService
FunctionscamelCasegetUserById
ConstantsUPPER_SNAKEMAX_RETRY_COUNT
Types/InterfacesPascalCaseUserProfile
Directorieskebab-caseuser-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/ or lib/

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'

Pergame Knowledge Base