Bit's World

Hope the best,plan the worst!

Why does someone perfer #if defined to #ifdef

I found this problem when my colleague review my code. They prefer to use the *#if defined* macro, so I googled it and find some discussion on it.

http://www.velocityreviews.com/forums/t290326-why-does-someone-perfer-if-defined-to-ifdef.html

Here is the correct and formal answer:

#ifdef is a short form that doesn't allow more complex expressions.

The only real 'defect' is that it's a single condition being tested.
This makes it fine for header file guards:

#ifndef H_MYHEADER
#define H_MYHEADER
...
#endif

but not so useful for complex conditions:

#ifdef HAVE_MYHEADER
# if VERSION > 3
...
# endif
#endif

can often more clearly be written:

#if defined(HAVE_MYHEADER) && VERSION > 3
...
#endif

It's largely a matter of style preference. I like the #ifndef for
header include guards because it's easy to see that the macro being
tested is immediately defined, but for complex conditions I generally
prefer using defined().

(Whether you do defined FRED or defined(FRED) is also a style issue,
I've worked in places where one is compulsory and the other forbidden
but there is no logical reason to prefer one to the other as far as I
can see.)


But I like another answer that's quite interesting.

Indentifiers should be pronouncable, out-loud.

For example, someone had the brainless idea to prefix internet servers that
serve HTTP with 'www'. Nobody reviewed that prefix for pronouncability,
meaning today radio personalities suffer when their programming formats
forbid "dub-dub-dub" or "triple-double-you". They must say "double-you
double-you double-you" all day.

So, read your code out loud, including the #ifdef, and listen for if it
makes sense...

Also someone mentioned how *www* are pronounced in other languages:

It's easy in German: "vay vay vay". Not too bad in Cymraeg (Welsh): "oo
oo oo". In English I've heard "wuh wuh wuh"...


syscall() != syscall wrapper in libc.Get timestamp by rdtsc

Write a comment

New comments have been disabled for this post.

May 2013
S M T W T F S
April 2013June 2013
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31