4 ms·
Could you please share why wrapping structs in a typedef is bad? I'm culpable of doing this quite often.
by fathyb 4y ago
Could you please share why wrapping structs in a typedef is bad? I'm culpable of doing this quite often.
- spacechild1 4y agoIt is common practice and IMO not bad at all.
- enriquto 4y agoIt is largely considered bad and misleading style. Why would you ever hide that a variable is a srruct? Better to keep typedef only for basic types and for function pointers.
- detrites 4y agoHow is it hidden? Can't the inquiring mind simply examine the codebase to see it's a struct? And if they choose not to, isn't it on them if it turns out to be something other than what they assumed it was without looking?
- moloch-hai 4y agoIt is hidden because you have to "examine" somewhere else before you know. But it doesn't matter, because you don't need to know unless it is subject to invisible implicit conversions. What might you do differently, having "examined" the typedef?
- Warwolt 4y agoI understand the preference for keeping the struct tag, but virtually no other language does this, with modern IDEs it's trivial to find out what the definition of a given type is, so the tag seems superfluous to me in practice.
- Jtsummers 4y agoWe don't even need modern IDEs. ctags solved this problem last century.
- rm445 4y agoI think so. Even with a compiled library distributed with a header file but no source, the header file needs to forward declare something like 'typedef struct foo FOO;' So (unless there's some clever trick I've missed) you can always tell it's a struct but not necessarily see the definition. There is a point, perhaps a little specious, that you put a module's data structures behind a typedef so the interface doesn't change if it changes from a simple data type to a struct. Probably doesn't happen too often. The perfect C object-oriented-style interface is FILE* from stdio.h. A FILE is a structure full of operating-system-specific file information but you never have to see it or worry about what's in it, you just use the functions.
- heywhatupboys 4y ago> There is a point, perhaps a little specious, that you put a module's data structures behind a typedef so the interface doesn't change if it changes from a simple data type to a struct. Probably doesn't happen too often. you could never do this in C. If it is a "value type" i.e. a non-pointer, then you cannot change the size of the value, without changing the ABI and the function decl.
- moloch-hai 4y agoThe only bad or misleading typedef is one that conceals that a type is a pointer or reference, and therefore implicitly converts to another type. Technically, int32_t is bad in that way, but we are not fooled by it.
- unsafecast 4y ago> Technically, int32_t is bad in that way, but we are not fooled by it. Meaning? On my system int32_t is directly typedefed to unsigned int, absolutely no hidden pointers or conversions.
- moloch-hai 4y agoint32_t promotes to long, converts to unsigned, and truncates to short and char, all silently.
- deschutes 4y agoThe struct tag is an idea that's survived into approximately zero other languages. It's largely visual clutter.
- properparity 4y agoWhy would I ever care if something is a struct or not a struct? Just that is almost zero useful information. I do care about the size of a struct sometimes, but that would require me to go to the definition of the struct, so just seeing the word "struct" didn't help me one bit. And I of course care about the members of the struct, but that again requires me to know what the actual members are which the word "struct" doesn't give me. So what exactly does omitting the word "struct" hide again?
- rramadass 4y agoThere is nothing wrong with either practice. The GP is just stating their preferred style. In fact it is good practice to use struct wrappers for void* pointers to get type safety. On the other hand, a typedef is just for programmer convenience and the compiler doesn't care. Eg: See DECLARE_HANDLE defined under STRICT at https://renenyffenegger.ch/notes/Windows/development/WinAPI/data-types/index https://renenyffenegger.ch/notes/Windows/development/WinAPI/...
- hot_gril 4y agoIt's not necessarily bad, but personally I hardly ever do it, and at work the style guide says no. Gets annoying for other readers to keep checking what something really is, and even for the writer it's usually just extra toil that won't really help you. Like, my variable is already declared as `int meters` probably. I don't need the redundancy of saying `meters_t meters`. Maybe I even want to store meters as a `long` in certain contexts.