Documentation
Arx Code Style
Arx Code Style
Use these conventions when writing Arx source for libraries, applications, examples, and tests. The guide draws on PEP 8 and follows Arx's lexical syntax and Douki docstrings. Apply the conventions to new and edited code. Keep formatting changes to unrelated code out of feature and fix diffs.
Layout
- Use two spaces per block level and write source as UTF-8. Start at column one at the module level.
- Use two blank lines around top-level
fnandclassdefinitions when there is adjacent source. Keep an@<...>template declaration immediately above its definition and count the blank lines before the template. - Use one blank line between methods and between fields and the first method. Keep related fields together. Inside a function, use a blank line when it separates distinct steps.
- Aim for at most 79 characters per line. Keep a literal or URL intact when splitting it would change its value or make it harder to read.
- Wrap long parameter lists, calls, and grouped expressions inside parentheses.
Indent the continuation two spaces from the line opening the group and align
the closing parenthesis with that line. For a wrapped signature or call, put
one parameter or argument on each continuation line. A function body starts
one block level under the
fndeclaration. - Remove trailing whitespace and end each file with a newline.
Module order and imports
Start with the module docstring. Follow it with imports, then module constants and type aliases, and then definitions. Leave one blank line between the docstring and the imports, and between groups of imports. Group imports in this order.
- Arx standard library and explicit builtin imports
- External package imports
- Imports from the current project
Use the name-first import name from module form for imported symbols. Import
related names from the same module on one line when they fit, using parentheses
for a group. Use absolute module paths across package boundaries. Use relative
imports for nearby modules in the same package when that makes the relationship
clear. See Modules for the supported forms.
Names and visibility
| Use | Convention | Example |
|---|---|---|
| Modules and packages | Short lowercase names, with underscores where useful | file_tools |
| Functions, methods, variables, fields, and parameters | snake_case |
read_score, max_score |
| Classes and named types | PascalCase |
ScoreLimit |
| Module constants | Uppercase words separated by underscores | MAX_SCORE |
Use self as the first parameter of instance methods. Use Arx visibility
modifiers to express access, such as -> private :: i64 for a private function.
Use a double-underscore name only for an established language protocol, such as
__init__.
Spacing and expressions
- Write declarations as
count: i64 = 0. Put a space after the colon in annotations and commas in lists, and put spaces around=in declarations and assignments. - Put spaces around binary operators and
->in signatures. - Keep call and subscript delimiters next to their names. Use
score.bounded(limit=10),items[0], andtext[start:stop]with compact keyword arguments and slices. - Use
true,false, andnonefor their respective values. Write Boolean logic withand,or, andnot. Test a Boolean directly withif ready:orif not ready:. - Use double quotes for strings and single quotes for individual character literals. Each quote style has its own meaning in Arx.
Comments and Douki docstrings
Begin every source file with a Douki module docstring at line one, column one.
Place class, function, and method docstrings first in their bodies. Use triple
backticks around the YAML and include a title. Document what callers need to
know, such as accepted inputs, units, ownership, and errors. See
Docstrings for the supported fields and placement rules.
Write comments with # and a following space. Prefer a comment immediately
above the relevant code for reasoning or constraints that a reader might miss.
When a short inline comment helps, separate it from the statement by two spaces.
Complete example
```
title: Score limits
summary: Apply an inclusive upper bound to a score.
```
import math from stdlib
MAX_SCORE: const :: i64 = 10
class Score:
```
title: Score value
```
value: private :: i64
fn __init__(self, value: i64) -> none:
self.value = value
fn bounded(self, limit: i64 = MAX_SCORE) -> i64:
```
title: Clamp the score to the inclusive range from zero to the limit.
parameters:
limit: Nonnegative upper bound.
returns: The score after clamping.
```
return math.clamp(self.value, 0, limit)
fn reached_limit(
score: Score,
limit: i64
) -> bool:
return score.bounded(limit=limit) == limit
fn main() -> i32:
score: Score = Score(15)
assert reached_limit(score, limit=MAX_SCORE)
return 0