Skip to main content

Style Guide

Optional Values

  • Optional values should be prepended with maybe_ and unwrapping should follow the if (maybe_x) |x| {} format
  • For example:
fn do_something(maybe_foo: ?Foo) void { 
if (maybe_foo) |foo| {
// do something with foo here
}
}

Function Signatures

  • If passing an Allocator as a parameter, it should be the first parameter of the function
  • If the number of possible errors which a function can return is reasonably small, then the error types should be explicit instead of using anyerror and the ! operator
    • For example, if the function can only fail on memory allocations, then the error should be error{ OutOfMemory }
  • If a parameter is not modified, then it should be const (eg, fn get(*const Self))

Slices

  • When converting an array from a slice, the syntax &buf should be used instead of buf[0..]

Writing Tests

  • When writing tests the naming convention is: test "{path to file}: {test name}"
    • For example, in src/gossip/data.zig a test is defined as test "gossip.data: test label() and id() methods"

Linting

  • Run zig fmt ./ in the top-level directory to run the zig linter

Removing Unused Imports

  • Run python remove_unused.py src/ from the top-level directory to remove unused imports