By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,156 Members | 995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,156 IT Pros & Developers. It's quick & easy.

Why does someone perfer #if defined to #ifdef

P: n/a
Do #ifdef or #ifndef have some defects? I ever heard that some people
use #if defined() or #if !defined() instead of using #ifdef or #ifndef
in header file.

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
* lovecreatesbeauty:
Do #ifdef or #ifndef have some defects? I ever heard that some people
use #if defined() or #if !defined() instead of using #ifdef or #ifndef
in header file.


#ifdef is a short form that doesn't allow more complex expressions.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

P: n/a
lovecreatesbeauty wrote:
Do #ifdef or #ifndef have some defects? I ever heard that some people
use #if defined() or #if !defined() instead of using #ifdef or #ifndef
in header file.


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...

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
Jul 23 '05 #3

P: n/a
lovecreatesbeauty wrote:
Do #ifdef or #ifndef have some defects? I ever heard that some people
use #if defined() or #if !defined() instead of using #ifdef or #ifndef
in header file.


#ifdef is shorter form of #if defined. But #if defined offers you more
flexibility than #ifdef. e.g.

#if defined(x) || defined(y)
...
// code
#else
...
#endif

Above is not possible using #ifdef.

Krishanu

--

"Never argue with Dan Pop. And I do mean never. I think he may have
been wrong once, when he thought he was mistaken."
--Dann Corbit
Jul 23 '05 #4

P: n/a
On 20 Apr 2005 22:56:43 -0700, lovecreatesbeauty
<lo***************@gmail.com> wrote:
Do #ifdef or #ifndef have some defects? I ever heard that some people
use #if defined() or #if !defined() instead of using #ifdef or #ifndef
in header file.


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.)

Chris C
Jul 23 '05 #5

P: n/a
On Thu, 21 Apr 2005 06:25:55 GMT, Phlip
<ph*******@yahoo.com> wrote:
lovecreatesbeauty wrote:
Do #ifdef or #ifndef have some defects? I ever heard that some people
use #if defined() or #if !defined() instead of using #ifdef or #ifndef
in header file.
Indentifiers should be pronouncable, out-loud.


I think hrvastki is pronouncable easily ("hash ifndef" even easier, as
in "pie'n'chips"). But I have a Czech friend so I may have different
ideas about what is pronouncable <g>.
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.
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"...
So, read your code out loud, including the #ifdef, and listen for if it
makes sense...


I say a lot of code (not necessarily out loud, but I pronounce it to
myself). For instance:

x = (i > 1 ? fred : bill);

I say as "x equals if i greater than one then fred, else bill". Which
is a reason I tend to avoid very complex expressions, if I can't say it
clearly and meaningfully then it should probably be broken down into
simpler expressions.

In the case of nested #ifdefs being replaces by #if defined(...), the
same applies:

ifdef FRED then
ifdef BILL then
ifndef JOE
...

versus:

if defined FRED and defined BILL and not defined JOE ...

Chris C
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.