4 ms·
The tip about using function pointers to iterate a list is good but... macros, really?
by CrLf 16y ago
The tip about using function pointers to iterate a list is good but... macros, really?
- cturner 16y agoAre macros are inherently evil? If you had a convention (e.g. only capitalised words in macros, none in your program propper) then they could exist safely. Perhaps a good DSL could sit on top of C macros to shield developers from the syntax. Are there any good preprocessors that do this?
- arethuza 16y agoI don't think macros are inherently evil - but they certainly facilitate Lovecraftian horrors. I have seen production embedded code where the authors didn't really like C and decided that vaguely Pascal like syntax would be better and did this through macros - probably vaguely influenced by the original Bourne shell sources but with a darker madder twist.
- sdevlin 16y agoIt seems like you could say that about any sufficiently powerful construct. Sharper knives require better judgment to be used safely. This is why we often give novices rules of thumb like "don't use macros" or "don't use goto." I've seen some people criticize this practice, but I think it's actually a good thing; when you're just starting out, you don't know when these things are really called for. Working under some constraints simplifies things. With experience, you learn the pain points well enough to outgrow those rules and use macros/goto/whatever when called for.
- roel_v 16y agoNah, macros are cool. Of course you need to use them properly, you can use operator overloading the wrong way too, and you can drink beer to have a good night out with friend or you can become the drunk on the main square asking for nickles from passers by. A nice macro which implements foreach() for C++ is in boost, (BOOST_FOREACH), in foreach.hpp. It's documentation page has an overview of the design considerations that went into making this a 'safe' macro. It's quite informative for learning what to look out for when writing macros.
- silentbicycle 16y agoChicken (http://www.call-cc.org/ http://www.call-cc.org/), or anything that compiles to C in general.