6 ms·
This is probably an amateur C question, but would the typedef in the winning entry be considered good practice if you want to swap the type easily? Like being a
by squeaky-clean 11y ago
This is probably an amateur C question, but would the typedef in the winning entry be considered good practice if you want to swap the type easily? Like being able to change to a long double if a detector with higher precision is made, or int after noticing that all incoming data are ints anyways? I'd also probably give the type more descriptive name (which breaks the beautiful bug). Or would there be a better way to handle that sort of situation?
- nkurz 11y agoThat's a fine question to ask. Yes, using a typedef for this purpose is good practice. It's debatable if it's a good idea to add the abstraction of a typedef if you don't need the flexibility, but if you are trying to write generic code in C, a typedef like this is the best approach. The only real alternative would be a a preprocessor define, which is even more "heavyweight". A define written in uppercase might be better if you want to signify that it's something changeable, but the typedef is probably slightly safer. The real issue here is not the typedef, but the name choice. "float_t" is a reserved name, and it's unlikely to be a good idea to typedef a reserved name. Counter to common practice, all typenames ending with "_t" are actually reserved by Posix[1] and thus arguably should be avoided for user code. [1] http://www.gnu.org/software/libc/manual/html_node/Reserved-Names.html http://www.gnu.org/software/libc/manual/html_node/Reserved-N...