xiaohongshu
xiaohongshu
kelley-zig-philosophy
Write Zig code in the style of Andrew Kelley, creator of Zig. Emphasizes simplicity, explicit behavior, compile-time metaprogramming, and being a better C. Use when writing systems code that prioritizes clarity and safety.
Full skill instructions
Andrew Kelley created Zig to address the shortcomings of C and C++ while maintaining their strengths. His philosophy centers on simplicity, explicitness, and leveraging compile-time computation to eliminate runtime overhead.
"Zig is not trying to be Rust. Zig is trying to be a better C."
"The language should not have hidden control flow."
"Communicate intent to the compiler and other programmers."
Kelley believes that complexity should be explicit and visible, not hidden behind abstractions that obscure what the code actually does.
No Hidden Control Flow: What you see is what executes.
No Hidden Allocations: Memory operations are explicit.
Compile-Time Over Runtime: Move computation to compile time.
Simplicity Over Features: Small, orthogonal feature set.
comptime to eliminate runtime overheaddefer for cleanup/// doc commentscomptime over runtime genericsdefer over manual cleanup// comptime: evaluated at compile time, zero runtime cost
fn fibonacci(comptime n: u32) u32 {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
// This is computed at compile time
const fib_10 = fibonacci(10); // 55, no runtime computation
// Generic programming with comptime
fn max(comptime T: type, a: T, b: T) T {
return if (a > b) a else b;
}
const result = max(i32, 5, 10); // Type-safe, zero overhead
// Compile-time type reflection
fn printFields(comptime T: type) void {
const fields = @typeInfo(T).Struct.fields;
inline for (fields) |field| {
@compileLog(field.name);
}
}
// Errors are values, not exceptions
const FileError = error{
NotFound,
PermissionDenied,
Unexpected,
};
fn readFile(path: []const u8) FileError![]u8 {
// Return error or success
if (path.len == 0) {
return error.NotFound;
}
// ... read file
return data;
}
// Caller must handle errors explicitly
pub fn main() void {
const data = readFile("config.txt") catch |err| {
switch (err) {
error.NotFound => std.debug.print("File not found\n", .{}),
error.PermissionDenied => std.debug.print("Access denied\n", .{}),
else => std.debug.print("Unexpected error\n", .{}),
}
return;
};
// Use data...
}
// try: shorthand for catch and return
fn processFile(path: []const u8) !void {
const data = try readFile(path); // Propagates error if any
// Process data...
}
// errdefer: cleanup only on error
fn allocateAndProcess(allocator: Allocator) !*Resource {
const resource = try allocator.create(Resource);
errdefer allocator.destroy(resource); // Only runs if error occurs
try resource.init(); // If this fails, resource is freed
return resource;
}
const std = @import("std");
const Allocator = std.mem.Allocator;
// Always pass allocator explicitly
fn createBuffer(allocator: Allocator, size: usize) ![]u8 {
return allocator.alloc(u8, size);
}
fn processData(allocator: Allocator, input: []const u8) ![]u8 {
var result = try allocator.alloc(u8, input.len * 2);
errdefer allocator.free(result);
// Process...
return result;
}
pub fn main() !void {
// Choose your allocator
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const buffer = try createBuffer(allocator, 1024);
defer allocator.free(buffer);
// Use buffer...
}
fn processFile(path: []const u8) !void {
const file = try std.fs.cwd().openFile(path, .{});
defer file.close(); // Always closes, even on error
var buffer: [4096]u8 = undefined;
const bytes_read = try file.read(&buffer);
// Process buffer...
}
// Multiple defers execute in reverse order
fn complexOperation() !void {
const a = try acquireResourceA();
defer releaseResourceA(a);
const b = try acquireResourceB();
defer releaseResourceB(b);
const c = try acquireResourceC();
defer releaseResourceC(c);
// On exit (success or error):
// 1. releaseResourceC
// 2. releaseResourceB
// 3. releaseResourceA
}
// Slices: pointer + length, safer than raw pointers
fn processBytes(data: []const u8) void {
for (data) |byte| {
// Safe iteration, bounds checked in debug
std.debug.print("{x}", .{byte});
}
}
// Slice operations
fn example() void {
const array = [_]u8{ 1, 2, 3, 4, 5 };
const slice = array[1..4]; // [2, 3, 4]
const from_start = array[0..3]; // [1, 2, 3]
const to_end = array[2..]; // [3, 4, 5]
// Sentinel-terminated slices for C interop
const c_string: [:0]const u8 = "hello";
}
// Convert between pointer types explicitly
fn pointerConversions(ptr: [*]u8, len: usize) void {
const slice = ptr[0..len]; // Many-pointer to slice
const single = &ptr[0]; // Many-pointer to single pointer
}
const Point = struct {
x: f32,
y: f32,
// Methods are just namespaced functions
pub fn distance(self: Point, other: Point) f32 {
const dx = self.x - other.x;
const dy = self.y - other.y;
return @sqrt(dx * dx + dy * dy);
}
pub fn zero() Point {
return .{ .x = 0, .y = 0 };
}
};
// Usage
const p1 = Point{ .x = 0, .y = 0 };
const p2 = Point{ .x = 3, .y = 4 };
const dist = p1.distance(p2); // 5.0
const origin = Point.zero();
// Optional: T or null, explicit handling required
fn findUser(id: u32) ?User {
if (id == 0) return null;
return users[id];
}
pub fn main() void {
// Must handle null case
if (findUser(42)) |user| {
std.debug.print("Found: {s}\n", .{user.name});
} else {
std.debug.print("User not found\n", .{});
}
// orelse: provide default
const user = findUser(42) orelse User.anonymous();
// .?: unwrap or undefined behavior (debug trap)
const user = findUser(42).?; // Crashes if null in debug
}
Kelley approaches systems programming by asking:
comptime to shift workcomptime for zero-cost genericsdefer/errdefer for cleanupxiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience