Lesson 1 of 9 · 23m
Zig's Explicitness: No Hidden Machinery
Build the day-one mental model: allocation is visible, control flow is visible, dependencies are visible, and the language refuses small conveniences that become large debugging bills.
The first thing I tell experienced programmers learning Zig is that the language is not trying to make your life easy for the first hour. It is trying to make the tenth production incident understandable. When memory allocation, failure, copying, and execution order are visible in the source, you can reason about a program without reconstructing hidden machinery in your head.
The bill always arrives somewhere
In languages with pervasive implicit behavior, small expressions can allocate memory, clone data, throw exceptions, spawn destructor work, lock runtime structures, or invoke conversion operators. That is convenient until you are inside a hot loop, an embedded target, a signal-sensitive path, or a memory corruption report at 2 AM.
Zig pushes the cost accounting into the program text:
const std = @import("std");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const input = try std.fs.cwd().readFileAlloc(allocator, "data.txt", 1024 * 1024);
defer allocator.free(input);
std.debug.print("read {d} bytes\n", .{input.len});
}You can see allocation, ownership, cleanup, failure propagation, and the maximum accepted input size. Nothing important is happening behind your back.
Explicit does not mean noisy forever
Zig's try is compact because propagation is boring; allocation is explicit because it is not boring. That distinction is the taste of the language. The language removes ceremony where the behavior is mechanically obvious and keeps ceremony where a human needs to make a resource decision.
fn loadConfig(allocator: std.mem.Allocator) !Config {
const bytes = try readLimit(allocator, "config.json", 64 * 1024);
defer allocator.free(bytes);
return parseConfig(bytes);
}Even without seeing readLimit, a reviewer knows this function can fail and can allocate. They also know the temporary bytes are released before parsing finishes.
Control flow is part of the API
Hidden control flow makes interfaces dishonest. A function that can jump halfway across the program through an exception has a different contract from one that returns error.InvalidInput. Zig error unions force that contract into the type:
const PortError = error{ Missing, Invalid, OutOfRange };
fn parsePort(text: []const u8) PortError!u16 {
if (text.len == 0) return error.Missing;
return std.fmt.parseInt(u16, text, 10) catch |err| switch (err) {
error.InvalidCharacter => error.Invalid,
error.Overflow => error.OutOfRange,
};
}Notice the translation. parseInt knows about characters and overflow; callers of parsePort receive a stable domain error set. That friction is useful: it makes you choose whether callers should see parser details or a semantic port error.
What to inspect as a reviewer
When reviewing Zig, ask four questions before style questions:
Who owns this memory?
What is its lifetime?
Which operations can fail?
Does this expression copy, allocate, block, or reorder work?If those answers are visible, the code is probably maintainable even when it is terse. If they require knowledge of framework magic, the codebase will not scale socially.
Field notes
deferruns in reverse order within the scope, so ownership bugs often appear when cleanup order is accidental rather than designed.errdeferis for undoing partially constructed state on failure; usingdeferfor that job often frees memory on success too.- Zig will not protect you from a bad ownership design; it just refuses to hide the design from you.
- Do not imitate Rust, Go, or C++ project structure blindly. Zig code is usually clearest when ownership is boring and local.
std.debug.printwrites to stderr and is not a logging framework. For a real utility, decide what belongs on stdout versus stderr early.
Was this lesson helpful?