6.4 KiB
6.4 KiB
Coding Conventions
Analysis Date: 2026-04-30
Naming Patterns
Files:
- Frontend React components and pages use PascalCase file names (for example
frontend/src/components/MobileEditModal.tsx,frontend/src/pages/MedicationsPage.tsx). - Hooks use
useXcamelCase naming in files and symbols (for examplefrontend/src/hooks/useMedications.ts,frontend/src/hooks/useScheduleController.ts). - Backend routes/services use kebab-case file names with domain suffixes (for example
backend/src/routes/medications.ts,backend/src/services/medications-service.ts). - Test files use
*.test.tsor*.test.tsxin dedicated test folders (for examplebackend/src/test/planner.test.ts,frontend/src/test/components/MobileEditModal.test.tsx).
Functions:
- Use camelCase names for functions and methods (for example
parseIntakesWithUnitsinbackend/src/services/medications-service.ts,loadMedsinfrontend/src/hooks/useMedications.ts). - Use verb-first names for side-effect operations (
loadMeds,deleteMed,uploadMedImageinfrontend/src/hooks/useMedications.ts).
Variables:
- Use camelCase for local variables and state (
refillHistoryExpanded,scheduleDays,showFutureDaysinfrontend/src/context/AppContext.tsx). - Constant maps and singleton keys use UPPER_SNAKE_CASE (
LOG_LEVELSinbackend/src/utils/logger.ts,APP_CONTEXT_SINGLETON_KEYinfrontend/src/context/AppContext.tsx).
Types:
- Type aliases and interfaces use PascalCase (
AppContextValueinfrontend/src/context/AppContext.tsx,TestContextinbackend/src/test/setup.ts). - Return-shape interfaces use
UseXReturnconvention for hooks (UseMedicationsReturninfrontend/src/hooks/useMedications.ts).
Code Style
Formatting:
- Tool used: Biome (
biome.json, scripts infrontend/package.json,backend/package.json,package.json). - Key settings from
biome.json:indentStyle: tabindentWidth: 2lineWidth: 120- JavaScript quote style is double quotes, semicolons enabled, trailing commas
es5.
Linting:
- Tool used: Biome linter (
biome.json). - Key rules enforced/relevant:
style.useConst: errorstyle.noNestedTernary: warncorrectness.noUnusedVariables: warnsuspicious.noExplicitAny: warn
- Project governance in
AGENTS.mdreinforces readable code, early returns, and no nested ternaries.
Import Organization
Order:
- Node built-ins first in backend modules (for example
node:pathinbackend/src/routes/medications.ts,node:cryptoinbackend/src/index.ts). - External packages second (
fastify,zod,drizzle-ormin backend;react,@testing-library/*in frontend). - Internal modules last with relative paths (
../db/client.js,../../types).
Path Aliases:
- Not detected in TypeScript configs (
frontend/tsconfig.json,backend/tsconfig.jsondo not definepaths). - Relative imports are the standard.
Error Handling
Patterns:
- Backend validates request data with Zod schemas and
.refine(...)constraints before route logic (backend/src/routes/medications.ts). - Backend route tests assert explicit status codes and body shape (
backend/src/test/routes-real.test.ts,backend/src/test/planner.test.ts). - Frontend hooks often normalize recoverable API errors into UI-safe states (
frontend/src/hooks/useMedications.tsconverts network failures intoNETWORK_ERROR). - Some frontend fetch flows still use tolerant fallbacks (
catch(() => setMeds([]))infrontend/src/hooks/useMedications.ts), so future changes should prefer explicit user-facing error channels perAGENTS.mdfail-clear guidance.
Logging
Framework:
- Backend startup logger wrapper over console with level filtering in
backend/src/utils/logger.ts. - Runtime HTTP logging via Fastify logger options in
backend/src/index.ts(buildLoggerOptions, request correlation IDs). - Frontend logging utility mirrors backend level semantics (
frontend/src/utils/logger.ts).
Patterns:
- Central log-level maps (
LOG_LEVELS) andshouldLoggating are standard in both frontend and backend logger modules. - Correlation ID propagation is enforced at request boundaries (
backend/src/index.tsonRequest hook settingx-correlation-id).
Comments
When to Comment:
- Comments are used for rationale and test setup intent, not line-by-line narration.
- Typical examples:
- Migration/setup intent in
backend/src/test/setup.ts - E2E stability rationale in
frontend/e2e/fixtures/index.ts - Timeout/determinism notes in
frontend/vitest.config.tsandfrontend/playwright.base.config.ts
- Migration/setup intent in
JSDoc/TSDoc:
- Used selectively for exported utilities and test helpers (
backend/src/test/setup.ts,frontend/e2e/fixtures/index.ts,frontend/src/utils/logger.ts). - Not mandatory for every function; concise type annotations plus targeted comments are preferred.
Function Design
Size:
- Small-to-medium focused functions are common in services/hooks (
parseRawIntakeUnits,normalizeDateTimeinbackend/src/services/medications-service.ts). - Larger orchestrator modules exist where domain aggregation is required (
frontend/src/context/AppContext.tsx).
Parameters:
- Object parameters are used for extensibility in test factories and route payload shapes (
CreateMedicationOptionsinbackend/src/test/setup.ts). - Explicit primitive parameters used for concise helpers (
clickEditMed(page, medName)infrontend/e2e/medication-edit.spec.ts).
Return Values:
- Explicit return types are common on exported functions (
Promise<TestContext>,UseMedicationsReturn). - Guard-clause returns are common for invalid input or unavailable state (
if (!intakesJson) return [];inbackend/src/services/medications-service.ts).
Module Design
Exports:
- Named exports are preferred for utilities, hooks, and service functions (
backend/src/services/notifications/index.ts,frontend/src/hooks/index.ts). - Mixed export style is used where legacy/default exports remain practical (
defaultexports in component barrelfrontend/src/components/index.ts).
Barrel Files:
- Barrel files are actively used for stable import surfaces:
frontend/src/components/index.tsfrontend/src/hooks/index.tsbackend/src/services/notifications/index.ts
- Practical rule for new code: export domain-level public APIs through local barrels, keep deep internal helpers imported directly.
Convention analysis: 2026-04-30