← ClaudeAtlas

zig-best-practiceslisted

Provides Zig patterns for type-first development with tagged unions, explicit error sets, comptime validation, and memory management. Must use when reading or writing Zig files.
aiskillstore/marketplace · ★ 329 · Data & Documents · score 79
Install: claude install-skill aiskillstore/marketplace
# Zig Best Practices ## Type-First Development Types define the contract before implementation. Follow this workflow: 1. **Define data structures** - structs, unions, and error sets first 2. **Define function signatures** - parameters, return types, and error unions 3. **Implement to satisfy types** - let the compiler guide completeness 4. **Validate at comptime** - catch invalid configurations during compilation ### Make Illegal States Unrepresentable Use Zig's type system to prevent invalid states at compile time. **Tagged unions for mutually exclusive states:** ```zig // Good: only valid combinations possible const RequestState = union(enum) { idle, loading, success: []const u8, failure: anyerror, }; fn handleState(state: RequestState) void { switch (state) { .idle => {}, .loading => showSpinner(), .success => |data| render(data), .failure => |err| showError(err), } } // Bad: allows invalid combinations const RequestState = struct { loading: bool, data: ?[]const u8, err: ?anyerror, }; ``` **Explicit error sets for failure modes:** ```zig // Good: documents exactly what can fail const ParseError = error{ InvalidSyntax, UnexpectedToken, EndOfInput, }; fn parse(input: []const u8) ParseError!Ast { // implementation } // Bad: anyerror hides failure modes fn parse(input: []const u8) anyerror!Ast { // implementation } ``` **Distinct types for domain concepts:** ```zig // Prevent mi