Why does someone perfer #if defined to #ifdef

L

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

Alf P. Steinbach

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

Phlip

lovecreatesbeauty said:
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...
 
C

Chris Croughton

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
 
K

Krishanu Debnath

lovecreatesbeauty said:
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
 
C

Chris Croughton

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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top