Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

That macros ruin everything they touch. I used them extensively for maybe 15 years, stopped adding them, and then a bit later removed them all from my code. My C/C++ code was a lot nicer without them.


Everything powerful is also potentially destructive. Macros have their places, but most of the time they are not the right solution. I too have removed most of my macros, and have a few that are worth to keep. Usually time saved with macro cleverness is paid off with time debugging with cryptic compiler behavior.

When there is no other way, wrapping multi-line macros in a do while and #define constants in parenthesis can definitely help.


My early C code would have been awfully slow without them. We needed enums and good inlining before we could ditch (most) macros. When did Zorland/Zortech C become good enough?

There are still a few special cases where macros are useful, such as the multiple #include trick where a macro #defined before the #include determines what the macro invocations in the include file does -- really helpful for building certain kinds of tables.


Zortech had an inliner even before it was Zortech.

The #include trick is called the "X Macro". I used it extensively, and eventually just removed it.


Was it reliable enough?

(I have never played with it -- I saw the ads in Byte but I never met anybody who had tried it. It seemed so ridiculously cheap that I felt it had to be a scam ;) )


It was frequently better and faster than the others. Mine was the first (for the PC) to add data flow analysis.


I agree, with this as it relates to C/C++, but I *love* rust macros when used judiciously.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: