Draft / Experimental: Zith documentation describes a language under active development. Check Implementation Status before relying on a feature.

21. Best Practices & Patterns

Note: Best practices in this chapter describe the intended programming model. Many underlying features (NRA, dyn, comptime, when) are spec-only. Verify feature availability in impl-status.md before applying a pattern.

21.1 Ownership Patterns

21.2 Optional & Failable Patterns

21.3 Context Patterns

21.4 Error Handling Patterns

21.5 Macro Patterns

21.6 Rule of Three

If a function needs more than three specialized tools (markers, words, contexts, macros, comptime, inline error handling), something went wrong. Split the function or reconsider your abstraction.

// Good — two tools: marker + word
flow fn process() {
    dock init { ... }
    step1 -> step2
}

// Warning sign — four tools in one function
flow fn process() {
    dock init { ... }          // marker
    use Math;                  // context
    use assert AS CHECK;       // word
    risky()!                   // inline error handling
    // Prefer: move the context/word usage to a wrapper function
}

The Rule of Three keeps code readable. Zith gives you many tools — you don't have to use them all at once.

21.7 Naming Conventions

Construct Convention Examples
Variables & functions camelCase playerHealth, getDamage, loadConfig
Components single word, lowercase rgb, color, file, vertex, health
Structs PascalCase Point, Container, DynArray, GameConfig
Traits & interfaces PascalCase Printable, iPositioned (interfaces use lowercase i prefix)
Files kebab-case game-loop.zith, asset-manager.zith
Constants & comptime UPPER_SNAKE_CASE MAX_SIZE, PI, DEFAULT_TIMEOUT
Enums PascalCase for the type; PascalCase for variants enum Direction { North, South }

Zith Language Specification — Draft v0.9