tag:blogger.com,1999:blog-5246987755651065286.post1785020994560203325..comments2024-02-22T16:15:42.388-08:00Comments on cbloom rants: 08-17-12 - Definescbloomhttp://www.blogger.com/profile/10714564834899413045noreply@blogger.comBlogger10125tag:blogger.com,1999:blog-5246987755651065286.post-66711023678568994742012-08-23T16:41:01.738-07:002012-08-23T16:41:01.738-07:00insert squinty Fry "not sure if like" me...insert squinty Fry "not sure if like" meme here.<br />cbloomhttps://www.blogger.com/profile/10714564834899413045noreply@blogger.comtag:blogger.com,1999:blog-5246987755651065286.post-89185086777572923852012-08-23T16:09:55.448-07:002012-08-23T16:09:55.448-07:00Quite by coincidence, I came across this just now:...Quite by coincidence, I came across this just now: <a href="http://www.codersnotes.com/notes/easy-preprocessor-defines" rel="nofollow">http://www.codersnotes.com/notes/easy-preprocessor-defines</a>.<br /><br />Can't quite decide what I think about it yet :) - but it looks like it should be fairly accident-proof.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5246987755651065286.post-47527383622899916962012-08-22T12:36:19.693-07:002012-08-22T12:36:19.693-07:00@nothings: gcc (and clang) has -Wundef to give a w...@nothings: gcc (and clang) has -Wundef to give a warning for symbols that aren't defined. I don't know if there's anything similar for MSVC.johnbhttps://www.blogger.com/profile/15875751919051082588noreply@blogger.comtag:blogger.com,1999:blog-5246987755651065286.post-47144946819245835092012-08-22T09:12:16.957-07:002012-08-22T09:12:16.957-07:00@ Tom - your point #1 reminds me of one of the big...@ Tom - your point #1 reminds me of one of the big wins of the #if method (I'm basically just repeating and agreeing with what you wrote) - <br /><br />when you're in a strange codebase and you see some #ifdef that toggles a bunch of code, you are often left wondering - hmm is this actually defined? It would be nice to have a way to run the compiler (in the real production build script) and then port the results back to the code to show you what parts of the code are actually live. (in big spaghetti codebases this can be very hard to track down). (you'd also like to be able to port back info from the linker to mark up the code with all the parts that were dead-stripped)<br /><br />With the #if method it's generally much easier, because you can grep for that symbol and hopefully find a clear place where it's set to 0 or 1.<br /><br />cbloomhttps://www.blogger.com/profile/10714564834899413045noreply@blogger.comtag:blogger.com,1999:blog-5246987755651065286.post-23570447496985277712012-08-22T09:07:57.315-07:002012-08-22T09:07:57.315-07:00@ Lars - what's my opinion? I think short gen...@ Lars - what's my opinion? I think short generic names in the global namespace are great! Of course they're terrible. (our macros are actually things like OODLE_MAKEDLL, but I removed the prefixes for this blog)<br /><br />@ nothings - I always thought that an #if on an undefined symbol was a silent zero, but I have found at least one compiler that generates an error for it. So you can neither rely on it being a silent zero nor can you rely on it even being a warning (many compilers don't even generate a warning). The only robust multi-platform solution is to manually check for #ifdef before every #if , which is mildly annoying but at least it's quite clear in the code.cbloomhttps://www.blogger.com/profile/10714564834899413045noreply@blogger.comtag:blogger.com,1999:blog-5246987755651065286.post-86610193026454540032012-08-21T17:07:38.780-07:002012-08-21T17:07:38.780-07:00(This would be an advantage because it would catch...(This would be an advantage because it would catch typos when you misspelled the name in the #if.)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5246987755651065286.post-40199244434095207682012-08-21T17:07:01.873-07:002012-08-21T17:07:01.873-07:00The biggest advantage of #if would be if #if warne...The biggest advantage of #if would be if #if warned you if the symbol was undefined, but I don't know of any compiler that does so.<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5246987755651065286.post-31451635450523065232012-08-21T08:54:27.472-07:002012-08-21T08:54:27.472-07:00I've recently switched to #if as well, in the ...I've recently switched to #if as well, in the last couple of weeks. I've been programming C/C++ for... quite a long time. I don't know why it's taken me so long to figure out the benefits of #if. Good thing I never claimed to be the sharpest tool in the box...<br /><br />Two other advantages I've noted, on top of your list:<br /><br />1. You can use Alt+G/Cmd-Option-J/M-./whatever to go to the place where the #define is defined, with all the usual working-on-other-people's-code type benefits. Doing this with a #ifdef only works if the define is actually defined...<br /><br />(Compared to doing a find-in-files, this saves you anywhere from a bit of hassle to a lot of hassle, depending on how much the #define is used.)<br /><br />2. You can perform arithmetic on the values to do a basic check that only one is defined. For example, "#if X+Y+Z!=1". It's a lot less typing than the #ifdef equivalent. (Unless I've been missing something all these years?) OK, so this isn't exactly foolproof, but I'm happy to use this if the check is right next to the list of mutually-exclusive defines, to trap simple errors.<br /><br />Now just a question of training my fingers, which still like to follow up every "#if" with a quick "def"...<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5246987755651065286.post-29260016834337598842012-08-20T12:34:42.674-07:002012-08-20T12:34:42.674-07:00What's your opinion on overly short and generi...What's your opinion on overly short and generic macro names?<br /><br />If I had a penny every time I had to tangle out the mess when several library vendors thought it to be a Good Idea[TM] to use macros like MAKEDLL and STATICLIB.<br /><br />I'd expect vendors and projects to have disambiguation warts, but that seems to be more an exception than the rule.Anonymoushttps://www.blogger.com/profile/13398136713424222031noreply@blogger.comtag:blogger.com,1999:blog-5246987755651065286.post-40624060745604676762012-08-20T12:23:55.938-07:002012-08-20T12:23:55.938-07:00"If some other bad part of the build script h..."If some other bad part of the build script has put a -DSTUFF on your command line, you can't easily undo that by just tacking something else on the end of the command line."<br /><br />You actually can: this is what the -U option is for.<br /><br />I completely agree though, #if is much better than #ifdef.johnbhttps://www.blogger.com/profile/15875751919051082588noreply@blogger.com