zig-best-practices

0xbigboss/claude-code · updated Apr 8, 2026

$npx skills add https://github.com/0xbigboss/claude-code --skill zig-best-practices
0 commentsdiscussion
summary

Follows type-first, functional, and error handling patterns from CLAUDE.md. This skill covers Zig-specific idioms only.

skill.md

Zig Best Practices

Follows type-first, functional, and error handling patterns from CLAUDE.md. This skill covers Zig-specific idioms only.

Type System Patterns

Tagged unions for mutually exclusive states — prevents invalid combinations that a struct with multiple nullable fields would allow:

const RequestState = union(enum) {
    idle,
    loading,
    success: []const u8,
    failure: anyerror,
};

Explicit error sets — documents exactly what can fail; anyerror hides failure modes:

const ParseError = error{ InvalidSyntax, UnexpectedToken, EndOfInput };
fn parse(input: []const u8) ParseError!Ast { ... }

Distinct types for domain IDs — compiler prevents mixing up different ID types:

const UserId = enum(u64) { _ };
const OrderId = enum(u64) { _ };

Comptime validation — catch invalid configurations at compile time, not runtime:

fn Buffer(comptime size: usize) type {
    if (size == 0) @compileError("buffer size must be greater than 0");
    return struct { data: [size]u8 = undefined, len: usize = 0 };
}

Memory Management

  • Pass allocators explicitly to every function that allocates; no global allocator state.
  • Place defer resource.deinit() immediately after acquisition — keeps cleanup co-located with creation.
  • Use errdefer for cleanup on error paths; defer for unconditional cleanup.
  • Use arena allocators for batch/temporary work; they free everything at once.
  • Use std.testing.allocator in tests — reports leaks with stack traces.
fn createResource(allocator: std.mem.Allocator) !*Resource {
    const resource = try allocator.create(Resource);
    errdefer allocator.destroy(resource);  // runs only on error
    resource.* = try initializeResource();
    return resource;
}

Key Conventions

  • Prefer const over var; prefer slices over raw pointers.
  • Prefer comptime T: type over anytype; explicit types produce clearer errors. Use anytype only for genuinely polymorphic cases (callbacks, std.debug.print-style).
  • Exhaustive switch: include an else returning an error or unreachable for truly impossible cases.
  • Use std.log.scoped(.module_name) for namespaced logging; define a module-level const log constant.
  • Larger cohesive files are idiomatic — tests alongside implementation, comptime generics at file scope.

Advanced Topics

  • Generic containers (queues, stacks, trees): See GENERICS.md
  • C library interop (raylib, SDL, curl): See C-INTEROP.md
  • Debugging memory leaks (GPA, stack traces): See DEBUGGING.md

Tooling

zigdoc — browse std library and dependency docs:

zigdoc std.mem.Allocator   # std lib symbol
zigdoc vaxis.Window        # project dependency
zigdoc @init               # create AGENTS.md with API patterns

ziglint — static analysis with .ziglint.zon config:

ziglint                    # lint current directory
ziglint --ignore Z001      # suppress specific rule

References

Discussion

Product Hunt–style comments (not star reviews)
  • No comments yet — start the thread.
general reviews

Ratings

4.632 reviews
  • Li Thomas· Dec 20, 2024

    Useful defaults in zig-best-practices — fewer surprises than typical one-off scripts, and it plays nicely with `npx skills` flows.

  • Shikha Mishra· Dec 12, 2024

    Useful defaults in zig-best-practices — fewer surprises than typical one-off scripts, and it plays nicely with `npx skills` flows.

  • Chinedu Mehta· Nov 11, 2024

    zig-best-practices has been reliable in day-to-day use. Documentation quality is above average for community skills.

  • Yash Thakker· Nov 3, 2024

    zig-best-practices has been reliable in day-to-day use. Documentation quality is above average for community skills.

  • Dhruvi Jain· Oct 22, 2024

    Solid pick for teams standardizing on skills: zig-best-practices is focused, and the summary matches what you get after install.

  • Yuki Desai· Oct 2, 2024

    Solid pick for teams standardizing on skills: zig-best-practices is focused, and the summary matches what you get after install.

  • Michael Park· Sep 21, 2024

    zig-best-practices is among the better-maintained entries we tried; worth keeping pinned for repeat workflows.

  • Rahul Santra· Sep 13, 2024

    zig-best-practices is among the better-maintained entries we tried; worth keeping pinned for repeat workflows.

  • Nia Zhang· Sep 13, 2024

    zig-best-practices fits our agent workflows well — practical, well scoped, and easy to wire into existing repos.

  • Camila Abebe· Sep 5, 2024

    Useful defaults in zig-best-practices — fewer surprises than typical one-off scripts, and it plays nicely with `npx skills` flows.

showing 1-10 of 32

1 / 4