3.2 KiB
title, last_modified
| title | last_modified |
|---|---|
| Code Styleguide | 2026-02-22 |
All
-
Every repo must have a
Makefileand aDockerfile. See Repository Policies for required targets and conventions. -
Credentials and/or secrets should never be committed to any repository, even private ones. Store secrets in environment variables, and if they are absolutely required, check on startup to make sure they are set/non-default and complain loudly if not. Exception, sometimes: public keys. (Public keys can still sometimes be secrets for operational security reasons.)
-
KISS & DRY: Keep it simple, stupid (KISS) and Don't Repeat Yourself (DRY) to minimize complexity and technical debt.
-
Composition over Inheritance: Prefer combining simple objects to build complex ones rather than creating deep class hierarchies.
-
Avoid nesting
ifstatements. If you have more than one level of nesting, consider inverting the condition and usingreturnto exit early. -
Almost all services/servers should accept their configuration via environment variables. Only go full config file if absolutely necessary.
-
For services/servers, log JSON to stdout. This makes it easier to parse and aggregate logs when run under
docker. Use structured logging whenever possible. You may detect if the output is a terminal and pretty-print the logs in that case. -
Debug mode is enabled by setting the environment variable
DEBUGto a non-empty string. This should enable verbose logging and such. It will never be enabled in prod. -
For services/servers, make a healthcheck available at
/.well-known/healthcheck. The response must have aContent-Type: application/jsonheader and return a JSON object containing the service's name, uptime, and a key of"status"with a value of"ok". Return a 200 for healthy, 5xx for unhealthy. -
If possible, for services/servers, include a /metrics endpoint that returns Prometheus-formatted metrics. This is not required for all services, but is a nice-to-have.
Bash / Shell
-
Use
[[instead of[for conditionals. -
Use
$( )instead of backticks. -
Use
#!/usr/bin/env bashas the shebang line. This allows the script to be run on systems wherebashis not in/bin. -
Use
set -euo pipefailat the top of every script. This will cause the script to exit if any command fails, or if a variable is used before it is set. -
Use
pvfor progress bars when piping data through a command. -
Put all code in functions, even a main function. Define all functions then call main at the bottom of the file.
Docker Containers (for services)
- Use
runitwithrunsvinitas the entrypoint for all containers. This allows for easy service management and logging. In startup scripts (/etc/service/*/run) in the container, put asleep 1at the top of the script to avoid spiking the cpu in the case of a fast-exiting process (such as in an error condition). This also limits the maximum number of error messages in logs to 86400/day.
Author
@sjdev <[sj@sjdev.co(mailto:sj@sjdev.berlin)>
License
MIT. See LICENSE.