Coding Standards
Source references:
Architecture- Logging and Metrics Guidelines.md;
Architecture- Logging Guidelines.md;
Architecture- Security Architecture.md;
principles in ../principles/README.md.
Scope
Baseline expectations for all code shipped in ION — application code, infrastructure code, tests, and tooling. Language-specific style guides live in each service repo; this document defines the platform-wide rules that every language guide must inherit.
Legend: MUST, SHOULD, MUST NOT.
1. Repository & code hygiene
- MUST every service lives in its own GitHub repo, registered in
Anvil/service-map.json(ADR-0024). - MUST trunk-based development on
main; changes via short-lived PR branches; PR-validation workflow blocks merge. - MUST conventional-commits + semantic-versioning for published artefacts.
- MUST NOT commit secrets, tokens, connection strings, private keys, tenant IDs, or personal identifiers. Verified by pre-commit + secret scan in CI.
- MUST every merge is signed / verified per repo policy.
- SHOULD every PR ≤ 500 lines of net change; larger changes split or explicitly justified.
2. Language, frameworks & dependencies
- MUST use platform-sanctioned languages: TypeScript / Node (services, MFEs, BFFs), C#/.NET (Azure Functions where preferred), Go / Java / TS / .NET (Temporal workflows). New languages require an ADR.
- MUST pin dependency versions in a lockfile checked into the repo.
- MUST dependency scan (SCA) in CI blocks build on High/Critical CVEs unless waived with expiry.
- MUST NOT vendor libraries that duplicate an Azure-native capability without an ADR exception (§21). Current exception list: Temporal.
- SHOULD prefer standard libraries and platform-standard packages (logging, config, HTTP client) — do not re-invent per service.
3. Code style
- MUST enforce style via linter + formatter in pre-commit + CI (ESLint + Prettier / dotnet-format / golangci-lint / etc.).
- MUST language-specific style guide in the service repo cites this document as its baseline.
- MUST every public function / method has a doc comment describing purpose + non-obvious constraints.
- MUST NOT add comments that restate the code. Comments describe why, not what.
- SHOULD prefer explicitness over cleverness.
4. Domain modelling
- MUST modelling follows DDD (§3): bounded contexts,
aggregates, invariants defined in
design/domains/. - MUST ubiquitous-language terms used unchanged in code
(
Incident,Ticket,Investigation,Customer) — no abbreviations, no synonyms. - MUST aggregates enforce their own invariants; no cross-aggregate transactions.
- MUST NOT leak infrastructure types (Cosmos response, SB
ServiceBusMessage) into the domain layer.
5. Events, commands, and idempotency
- MUST every event conforms to the platform API Standards §2.
- MUST every command handler is idempotent by key.
- MUST every subscription handler tolerates duplicate + out-of-order delivery.
- MUST publish domain events through the platform event helper (do not hand-craft CloudEvents envelopes per service).
6. Configuration
- MUST all configuration sourced from Azure App Configuration + Key Vault references. No environment-specific config baked into images.
- MUST every runtime toggle wired through Feature Flags.
- MUST NOT call
process.env/Environment.GetEnvironmentVariabledirectly from business logic — go through a config abstraction. - MUST limits, quotas, retry counts, timeouts all be configurable at runtime.
7. Secrets & credentials
- MUST every secret retrieved via Managed Identity → Key Vault at runtime (ADR-0013).
- MUST NOT log secrets, tokens, or Auth headers. Redaction middleware mandatory.
- MUST NOT store credentials in code, config files, container images, CI variables, or repo files.
8. Logging (structured)
- MUST structured JSON logs (one event per line).
- MUST every log entry includes:
timestamp,level,service,env,correlationId,traceId,spanId,customerShort/tenantId/workspaceId(where in scope),entityId(incidentId/ticketIdwhere applicable). - MUST log level configurable at runtime; default WARN.
- MUST NOT log PII, secrets, tokens, request bodies containing sensitive fields, or full stack traces to public sinks.
- MUST use levels correctly:
-
ERROR— unrecoverable failure requiring investigation -WARN— recoverable or degraded condition -INFO— significant lifecycle events -DEBUG— developer diagnostics, off in prod by default
9. Metrics
- MUST publish the 4 golden signals — Latency, Traffic, Errors, Saturation — per endpoint + per consumer (Observability).
- MUST business KPIs published as custom metrics (e.g.
incidents_processed_total,dashboard_freshness_seconds). - MUST metric names snake_case; dimensions bounded (never unbounded cardinality — no per-user, no per-incident-ID dimensions).
10. Tracing
- MUST OpenTelemetry SDK; propagate W3C
traceparentthrough HTTP, Service Bus, SignalR. - MUST every outbound dependency call is a span with standard attributes.
11. Error handling
- MUST typed / structured errors at module boundaries; no bare
throw "string"/panic("..."). - MUST every outbound call has: timeout, retries with exponential back-off + jitter, circuit breaker where dependency health matters.
- MUST NOT swallow errors silently. Every catch has a log or a compensation.
- MUST validate + sanitize every external input (API bodies, event payloads, config values).
12. Concurrency, state & memory
- MUST service is stateless at the compute layer; session state in Redis or the client.
- MUST graceful shutdown handler drains in-flight work on SIGTERM.
- MUST cancellation tokens / AbortController propagated through every async call.
- MUST NOT hold long-lived static caches without eviction policy.
13. Testing
- MUST unit tests for domain + command handlers; aim ≥ 80 % coverage on business modules.
- MUST integration tests for every REST endpoint + every event subscription.
- MUST contract tests for every produced + consumed event (schema conformance).
- MUST deterministic tests — no reliance on wall clock, network, or ordering.
- SHOULD property-based / fuzz tests on parsing and validation surfaces.
14. Containers
- MUST one process per container; handle PID 1 / signal forwarding correctly.
- MUST run as non-root user with read-only root filesystem where possible.
- MUST minimal base image (distroless / chiseled / alpine as per stack); remove build tooling from runtime layer.
- MUST image tagged with immutable digest for deploy;
latestnever deployed. - MUST vulnerability scan in CI blocks build on High/Critical CVEs.
15. Infrastructure code
- MUST Terraform via shared
Anvil/tf-modules/(ADR-0024, §25). - MUST state backend + locking configured per environment; no local state.
- MUST
terraform fmt,terraform validate,tflint,tfsec/checkovin CI. - MUST NOT manual portal changes above Alpha — reconciled or rolled back within one sprint.
16. CI/CD
- MUST platform-standard push-to-main + PR-validation GitHub Actions workflows (ADR-0024).
- MUST runners in the ION VNet with NIC-in-VNet for private deploy targets.
- MUST Automation Account custom protection rules for prod promotion.
- MUST end-to-end traceability: PR → commit → build → artefact → deploy → env.
- SHOULD artefacts signed + provenance attested.
17. Documentation-in-code
- MUST README in every repo covers: purpose, run/test locally,
dependency map, links to service page in
design/systems/. - MUST every architecture-affecting change ships with a doc
update in
ion-v3-arch/in the same PR. - SHOULD ADRs authored in service repo when the decision is service-local; promoted to platform if it affects other services.
Related
- Microservice Checklist — design/build gate
- ORR Checklist — go-live gate
- API Standards
- Documentation Standards
- Principles · Patterns · WA capabilities