Data-oriented programming starts by representing data for its intended purpose, then lets the operations and interfaces follow. The goal is not to eliminate every abstraction, but to avoid adding complexity the data does not need. section 6.3.4 section 6.8
Consider a recipe book that must manage state and save and load recipes. Keeping recipes and ingredients flat and minimal makes operations over the whole dataset straightforward. An alternative with individual recipe objects and mutable ingredient lists would make serialization more complicated. section 5.0 section 5.3.4
Listing 5.12 Massage and serialize
fn toJson(
self: RecipeBook, ally: Allocator, writer:
*std.Io.Writer) !void { #1
var recipe_id_pairs: std.ArrayList(
JsonRepr.RecipeIdPair) = .empty; #2
defer recipe_id_pairs.deinit(ally);
var recipe_entries = self.recipes.iterator(); #3
while (recipe_entries.next()) |entry| { #3
try recipe_id_pairs.append(ally, .{
.recipe_id = entry.key_ptr.*,
.recipe = entry.value_ptr.*,
});
}
const repr: JsonRepr = .{
.recipes = recipe_id_pairs.items,
.ingredients = self.ingredients.items,
.next_recipe_num = self.next_recipe_num,
};
var stringify: std.json.Stringify = .{ .writer = writer };
try stringify.write(repr); #4
}
The JSON conversion collects recipe IDs and recipes into pairs, then writes them alongside the ingredient array and next recipe number. section 5.3.4
Loading reverses the process: a JSON reader tokenizes the input, and an arena supports one-shot parsing. Strings must be duplicated into the recipe book’s own storage before the temporary arena is released. Flat data simplifies the transformation; it does not make lifetime management disappear. section 5.3.4
Choose operations against the expected shape and size of the data, not just their asymptotic cost. For a recipe book with thousands of recipes and tens of thousands of ingredients, an O(n) deletion can be a reasonable trade-off rather than a reason to build a more elaborate structure. section 5.3.3
Listing 5.9 Make it an iterator
const IngredientIterator = struct {
book: *const RecipeBook, #1
recipe_id: RecipeId,
index: usize = 0, #2
fn next(iter: *@This()) ?Ingredient {
return while (iter.index < iter.book.ingredients.items.len) { #3
const ingredient = iter.book.ingredients.items[iter.index];
iter.index += 1;
if (ingredient.recipe_id == iter.recipe_id) { #3
return ingredient;
}
} else null;
}
};
fn iterateIngredients(
self: *const RecipeBook,
recipe_id: RecipeId,
) IngredientIterator {
return .{ .book = self, .recipe_id = recipe_id };
}
An ingredient iterator scans the shared ingredient array and returns entries matching a recipe ID. It packages a simple access pattern without changing the underlying representation. section 5.3.3
The same approach can guide an interpreter: begin with natural representations, identify the core functions that act on them, and add abstraction only when it earns its keep. A project that naturally fits this style may need little more than a few data structures and functions. section 6.3.4





