liveBook · Manning

Separation of concerns

As explained in Systems Programming with Zig

Separation of concerns is a design principle that partitions a system so each component addresses a limited set of related responsibilities.

Separation of concerns limits the responsibilities and system dependencies of each piece of code. The payoff is practical: there is less to keep in your head, and a change in one environment is less likely to force a refactor elsewhere. section 4.1.5

Validate words without printing

A word-ladder validator can check length, word-list membership, and whether exactly one letter changed. It returns the valid word or an explicit error instead of printing a message. Printing inside the validator would be fine for a small console-only program, but it would tie the validation logic to the console and complicate reuse in a web or mobile interface. section 4.1.5

Listing 4.9 Invalid word errors
const InvalidWordError = error {
    BadLength,
    NotInWordlist,
    NotWordLadder,
};

fn validateWord(
    input: []const u8,
    last_word: [4]u8,
) InvalidWordError![4]u8 { #1
    // validation code ...
}

The explicit error set gives callers the failure information they need without making the validator responsible for displaying it. section 4.1.5

Draw boundaries early, then use the build system to reinforce them. In a CHIP-8 interpreter, the virtual machine can be separate from the interactive runtime that handles keyboard input and graphical output. The runtime needs a heavier dependency that the VM does not; keeping it out of the VM also makes tests compile slightly faster. section 6.3.1

Listing 6.2 A more modular build script
const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{}); #1
    const optimize = b.standardOptimizeOption(.{}); #1

    const vm_mod = b.createModule(.{ #2
        .root_source_file = b.path("src/vm.zig"),
        .target = target,
        .optimize = optimize,
    });

    const test_mod = b.createModule(.{
        .root_source_file = b.path("src/tests.zig"),
        .target = target,
        .optimize = optimize,
    });
    test_mod.addImport("vm", vm_mod); #3
}

The build script creates separate VM and test modules and explicitly imports the VM into the tests. section 6.3.1

Modules are one tool, not the whole principle. Namespaces, dependency injection, generics, and variable scoping also help limit what a piece of code must deal with. section 4.1.5 section 6.8

How Systems Programming with Zig covers it

How deep it goes
Explains boundary choices and their trade-offs through validation errors and modular build steps.
Examples
Uses a word-ladder validator and a CHIP-8 interpreter with separate VM and test modules.
What you can do after
Apply it via Zig's build system by separating modules and their dependencies.
How it connects
Connects modularity with dependency injection, generics, namespaces, variable scoping, and build systems.

Also explained in

Compare how 25 books explain separation of concerns →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Systems Programming with Zig. The section links point to the parts of the book it draws on.