Zig’s style guide suggests using two different styles for file names:
If the file (implicitly a struct) has top level fields, it should be named like any other struct with fields using
TitleCase
. Otherwise, it should usesnake_case
.
In general, I think this makes a lot of sense, except that there are files which logically are not structs.
Now, of course all .zig
files are actually structs, but I find myself putting enums or unions into their own files and I’ve always struggled to name these files in a way that is logical and clear to the user. Same problem applies to generic types, btw, which are functions of course.
Let’s suppose I had a huge enum of different event types:
const Event = enum { ... };
At some point I might want to put just that enum into its own file. What do I name that file? As per the style guide, it should be event.zig
(or at least something in snake_case
). But then when I want to use Event
I either need to include the file into a constant event
and then always write event.Event
, which is very silly, or I need to do this thing:
const Event = @import("event.zig").Event;
But how do I know to do that? As a consumer of that file, I always need to look inside of it to see how to import it properly.
I don’t like that.
That’s why I came up with a slightly different naming convention for myself and I’d like to share that with y’all.
When I create a file with no fields and exactly one public member, I name the file @typeName(T) ++ "_.zig"
! So our Event
enum from earlier would live in a file named Event_.zig
. When importing this file, you now know immediately that you shouldn’t just import the entire file into a constant but rather directly access a member of the same name. So the import would look like this now:
const Event = @import("Event_.zig").Event;
Much clearer!
This convention works for enums, unions, functions, or anything else really. And since the underscore is at the end of the file name, the sorting between files is not affected.
What do y’all think about this? Do you have your own convention for this? Can you spot any problems with mine? Do you consider this worth it or not? Let’s talk about it