by jackiesshirt · 2y ago · 0 comments
I used to agree with this but I have moved away from compound literals entirely except for global statics/const definitions.
Having a variable and explicit:
foo.x = whatever;
foo.y = something_else;
Leads to better debug experience imo, can set breakpoints and single step each assignment and have a name to put a watch on.One advantage of initialization via compound literals is that you can make the target immutable, and you won't accidentially get any uninitialized junk in unlisted struct members, e.g.:
const vec3 vec = { .x = 1.0, .y = 2.0 };
...vec.z will be default-initialized to zero, and vec doesn't need to be mutable.