You Must Fix Your Asserts (Zig)
7 hours ago
- #Zig
- #software-engineering
- #asserts
- Asserts are used to state pre/post conditions and invariants, offering better protection against mistakes than unit tests, especially when fuzzing.
- Zig's asserts use 'unreachable' for optimization and safety, and different build modes handle assertion failures differently (panic vs. undefined behavior).
- Zig's std.debug.assert is a function, not a macro, so arguments are always evaluated, allowing side effects without the disabling concerns of C/C++.
- Disabling asserts in production is criticized as a bad practice because it hides errors, leads to program misbehavior, and prevents discovery of incorrect assumptions.
- Keeping asserts enabled in prod helps detect wrong asserts early, avoiding cascading errors and vulnerabilities, while optimizing compilers can use asserts for performance gains.