Why does someone perfer #if defined to #ifdef

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lovecreatesbeauty

    Why does someone perfer #if defined to #ifdef

    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.

  • Alf P. Steinbach

    #2
    Re: Why does someone perfer #if defined to #ifdef

    * lovecreatesbeau ty:[color=blue]
    > 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.[/color]

    #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?

    Comment

    • Phlip

      #3
      Re: Why does someone perfer #if defined to #ifdef

      lovecreatesbeau ty wrote:
      [color=blue]
      > 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.[/color]

      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



      Comment

      • Krishanu Debnath

        #4
        Re: Why does someone perfer #if defined to #ifdef

        lovecreatesbeau ty wrote:[color=blue]
        > 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.
        >[/color]

        #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

        Comment

        • Chris Croughton

          #5
          Re: Why does someone perfer #if defined to #ifdef

          On 20 Apr 2005 22:56:43 -0700, lovecreatesbeau ty
          <lovecreatesbea uty@gmail.com> wrote:
          [color=blue]
          > 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.[/color]

          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_MY HEADER) && 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

          Comment

          • Chris Croughton

            #6
            Re: Why does someone perfer #if defined to #ifdef

            On Thu, 21 Apr 2005 06:25:55 GMT, Phlip
            <phlip_cpp@yaho o.com> wrote:
            [color=blue]
            > lovecreatesbeau ty wrote:
            >[color=green]
            >> 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.[/color]
            >
            > Indentifiers should be pronouncable, out-loud.[/color]

            I think hrvastki is pronouncable easily ("hash ifndef" even easier, as
            in "pie'n'chip s"). But I have a Czech friend so I may have different
            ideas about what is pronouncable <g>.
            [color=blue]
            > 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.[/color]

            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"...
            [color=blue]
            > So, read your code out loud, including the #ifdef, and listen for if it
            > makes sense...[/color]

            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

            Comment

            Working...