# Coding Standards
<!--
ref: @any-corp/engineering/coding-standards
type: protocol
tags: [engineering, code, standards, style, conventions]
triggers: [new service development, code review process, team onboarding, standardization needs]
trigger_prompt: When a team member needs guidance on coding practices, language choices, or error handling to ensure consistency and quality in their code.
version: 1
updated_at: 2026-04-30T17:25:41.667Z
-->

Opinionated. Some things are taste; these aren't. Disagreements go in a DR (`@any-corp/handbook/decision-records`).

## Languages — what we use, by service tier

| Tier | Default language | Why |
|---|---|---|
| Backend services | TypeScript on Node, with the Bun runtime where supported | One stack across services, fast, ecosystem mature |
| Backend (perf-critical) | Go | Reach for it when TS isn't enough; not by default |
| Backend (data / ML) | Python | Tooling concentration |
| Frontend (web) | TypeScript + React | Standard |
| Mobile | React Native + Swift / Kotlin where needed | Shared logic where possible |
| Scripts / one-offs | TypeScript with Bun, or Bash | See `@any-corp/engineering/scripting-conventions` |

New services start in TypeScript unless there's a real reason to deviate. The reason goes in a DR.

## Style — pick one and let the formatter run

| Language | Formatter | Linter |
|---|---|---|
| TypeScript / JavaScript | Prettier | ESLint with our shared config |
| Python | Black + Ruff | Ruff |
| Go | gofmt + goimports | golangci-lint |
| SQL | sqlfluff | sqlfluff |
| Markdown | Prettier | markdownlint |

Formatters run **on save** in the editor and **on push** in CI. CI is the source of truth — if it fails, you fail.

We don't argue tabs vs. spaces, line length, or import order. The formatter decides.

## Naming

- Variables / functions: `camelCase` in TS / JS; `snake_case` in Python; `PascalCase` for types and React components.
- Files: `kebab-case.ts` for utilities and modules; `PascalCase.tsx` for React components.
- Constants in TS: `SCREAMING_SNAKE` only for true compile-time constants.
- Avoid Hungarian notation, type prefixes (`IFoo`, `T_user`), and abbreviations that don't read aloud.

## Errors

- **Don't swallow errors.** No `catch { }`. Log it, rethrow it, or handle it specifically.
- **Throw for programmer errors.** Bad input from internal callers, invariant violations, etc.
- **Return for expected failures.** Network call failed, validation didn't pass — these aren't exceptions.
- **Don't use exceptions for control flow.** If "user not found" is a normal outcome, return it; don't throw.

## Comments

Default to **no comments**. Naming and structure should explain *what*. Add a comment when:

- The *why* is non-obvious (a workaround, a hidden constraint, a subtle invariant).
- The *what* is genuinely surprising and refactoring it isn't an option right now.
- A future reader would otherwise be confused.

Don't write:
- Comments that say what the code says.
- Multi-paragraph docstrings on every function.
- "Fixed bug in X" — that's what commits are for.
- "TODO: refactor this" without an issue link or a date.

## Tests

- **Unit tests:** for the business logic. Mock external dependencies; test the pure parts.
- **Integration tests:** for the seams. Hit a real database, a real queue, a real cache.
- **E2E tests:** sparingly, for the critical user flows.
- **Test files** live next to the code: `foo.ts` + `foo.test.ts`.

We don't chase 100% coverage. We do require:
- A test for every bug fix (one that fails before, passes after).
- A test for every new public function.
- A test for the happy path on every new feature.

## Errors and logging in production code

- **Structured logging.** JSON, with named fields, never `console.log("user " + id + " did X")`.
- **Never log secrets.** API keys, tokens, passwords, customer data above identifiers.
- **Log levels:**
  - `error` — something is broken; humans need to see this.
  - `warn` — unexpected but recoverable.
  - `info` — significant business events.
  - `debug` — disabled in prod by default.
- **Trace IDs** flow through every request. Log with the trace ID, search by it.

## Dependencies

- **Add a dependency only when the build-vs-buy answer is "buy."** Trivial helpers we write ourselves.
- **Top of mind:** weekly downloads, last release date, license, maintainer count, size.
- **Audit before adding.** `npm audit`, `pip-audit`, `go vet`. New high-severity finding blocks the merge.
- **Pin versions** in lockfiles. Don't merge a PR that updates 30 unrelated dependencies "as a chore."

## Database access

- **No raw SQL in business logic.** Use the ORM or query builder.
- **Raw SQL is fine in scheduled jobs, migrations, and reports** — but parameterized, always.
- **One transaction per HTTP request, max.** Long-running transactions are bugs waiting.
- **Migrations** are reviewed by a senior engineer. Backward-compatible by default.

## Security

- **Never trust user input.** Validate at the boundary; sanitize before output.
- **Authorization is explicit, not implicit.** Every endpoint has a clear "who can call this."
- **Secrets in the secret manager**, not in env files committed to the repo.
- **Customer data minimization.** Only what you need; only as long as you need it.
- See `@any-corp/it/ai-acceptable-use` for AI-specific rules.

## What we don't do

- We don't write 500-line functions. Split.
- We don't merge PRs over 800 changed lines without a stacking justification (`@any-corp/engineering/code-review`).
- We don't let formatter / linter warnings rot. Treat them like compiler errors.
- We don't hand-roll auth, crypto, or anything where you can pick a battle-tested library. The cost of the bug isn't worth the elegance of the rewrite.

---

*Related: `@content/web/lorekit/2026-11-07-agent-memory-opentelemetry-moment` · `@protocol/code-health` · `@any-corp/engineering/code-review` · `@any-corp/engineering/git-and-pr` · `@knowledge/frameworks/the-interpretability-tax`*

---

*You're reading a protocol on [protocol.supply](https://protocol.supply) — hosted, searchable knowledge for AI agents. Think Agent Skills, as an API: bootstrap once (`curl -sL https://api.protocol.supply/bootstrap`), search (`/search?q=…`), fetch (`/@repo/slug`).*

*Any skill can become a protocol, and any protocol installs as a local skill — tell your agent "install this as a skill" or see [`@protocol/protocol-to-skill`](https://api.protocol.supply/@protocol/protocol-to-skill).*