Conditionally Disabling Code with Comptime in Zig

Mitchell Hashimoto latest blog post: Conditionally Disabling Code with Comptime in Zig

10 Likes

great post

Importantly, this means I can use the adwaita library in the runtime conditions as well as the code block and I won’t get compiler errors if the library isn’t available or isn’t the right version or if I reference types that only exist in newer versions of the library.

This has been a double edged sword (ok, maybe just a stick) for me. The unused codepaths are gracefully avoided, but I have a hard time cleaning up real unused code after a refactor.

We’ll probably get some good automated tools to catch real unused code eventually though.

I’ve found kcov helpful for that. It’s very simple to use with Zig, and whether you have 100% line coverage or not (I encourage this) it will show uncompiled code as blank lines, that is, it doesn’t count code which doesn’t end up in the binary for coverage, one way or the other.

Clearly one still has to distinguish between e.g. platform-specific code which just didn’t happen to compile, and actual stranded/dead code. But that should be fairly obvious, especially if it’s your own repo.

1 Like