5 ms·
Over time I removed all the C preprocessor tricks from my own code and just wrote ordinary C in its place. Much better. I don't get it. If my macros produce s
by cperciva 7y ago
Over time I removed all the C preprocessor tricks from my own code and just wrote ordinary C in its place. Much better.
I don't get it. If my macros produce standards-compliant code and they make my code easier to read and understand, why shouldn't I use them?
My goal as a developer is to write clean performant bug-free code... not to make life easy for compiler developers.
- WalterBright 7y ago> If my macros produce standards-compliant code and they make my code easier to read and understand, why shouldn't I use them? The problem is they don't make code easier to read and understand. Worse, the unhygienic nature of C macros makes it hard to contain them. I haven't seen your code, so I'm speaking based on what I've seen of mine and others' code. If you dial it back, the person who has to deal with your code after you leave will appreciate it. More generally speaking, if you're doing metaprogramming with the C macro system, you've outgrown the language and should consider a more powerful one.
- deleted 7y ago[deleted]
- icedchai 7y agoI once worked at a place that had a platform specific "DEBUG" log macro. It worked something like: DEBUG(msg); Except, it was defined in such a way that you actually had to have two closing parens, like DEBUG(msg)); It looked syntactically invalid, but whatever the macro did required it. The entire code base was littered with WTFs like that...
- WalterBright 7y agoI hated debug macros. There just was never a clean way to write them. I was determined that D would not suffer from that problem. `debug` is a keyword in D, and you can do things like: debug printf("I got here\n"); and the printf only gets compiled in when compiling with -debug. (Any statement can be used after the printf.) Even better, semantic checks for debug statements are relaxed - for example, purity is not checked for them. Meaning you can embed debug printf's in functions marked 'pure', instead of having to use a monad.
- xondono 7y agoCan you assure that your preprocessor tricks always generate compliant code? For me writing embedded code is the ultimate test for your programming skills, since a lot of C toolchains for embedded devices (as in bare metal embedded) are unstable, only almost compliant and full of weird behaviors and hacks.
- cperciva 7y agoHere's an example: https://github.com/Tarsnap/libcperciva/blob/master/datastruct/elasticarray.h https://github.com/Tarsnap/libcperciva/blob/master/datastruc... As long as you invoke use it properly, e.g. ELASTICARRAY_DECL(PTRLIST, ptrlist, void *); there's no way it will create non-compliant code.
- pjmlp 7y agoBecause when you work on a team, not everyone is a C Gandalf, probably not even yourself a couple of months later when fixing a bug with everyone screaming that the system is down. Exaggerating here, but rule of thumb is that it takes twice to debug as it takes to write it, so how long do you want to take to do maintenance fixes?
- cperciva 7y agorule of thumb is that it takes twice to debug as it takes to write it Ok, so... macros which let me write code faster should help with debugging time as well? ;-)
- pjmlp 7y agoIt is an exponential curve with cleverness as input parameter. So it depends how complex they are.
- gray_-_wolf 7y agoDebugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. --Brian Kernighan