|
|
|
|
|
by alecco
5950 days ago
|
|
It's better not to use macros. Let the compiler optimize. If you really need more control use __inline. Debugging code generated from complex macros is close to impossible. If you have to use someone else's macros you can use a wrapper function with exactly the same effects. Also avoid #define constants not related to compilation or pre-processing themselves. Readable code > unreadabe code. Same counts for compiled binaries. |
|
I have the following macro in a program I'm writing right now:
Turning this into a function is impossible: functions can't define functions in C.Here's another one:
Ignoring the function composition problem, assume you want to implement the semantics here.If I type this nonsense out for the 60 (or so) times its used, it is likely I'll make a typo that will be difficult to catch.
If I build the table at run-time (roughly the same amount of code), then I accept a run-time cost for something that will be called very frequently.
If I build a table at compile time and fill it at startup, then my program takes longer to start. Maybe if I put a splash screen up my users will forgive me, but that seems dishonest.
A long time ago, programmers did abuse macros as a kind of poor-compiler's inlining, and your advice is good for that: people shouldn't be doing that, and legacy code where it exists should be fixed.
However macros are great for composition. They can greatly improve the readability of some otherwise repetitive code, and while they can be used for evil, they could also be used to solve problems.