Do you think this macro is a bad programming practice?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Adrian20XX
    New Member
    • Feb 2008
    • 11

    Do you think this macro is a bad programming practice?

    Hi,

    I want to define a macro in C like this, but I wonder if these is too obscure and I'm starting to get too nasty.

    This is the macro I want to use:
    Code:
    #define CompareXYAndDoActions(X, Y, greaterAction, equalAction, lowerAction)			\
    {																					\
    	if (X>Y) {																		\
    		greaterAction;																\
    	}																				\
    	if (X==Y) {																		\
    		equalAction;																\
    	}																				\
    	if (X<Y) {																		\
    		lowerAction;																\
    	}																				\
    }
    Later I plan to call it like this:
    Code:
    CompareXYAndDoActions(X, Y,
    		{
    			Action1;
    		},
    		{
    			Action2;
    		},
    		{
    			Action3;
    		}
    		);
    So, do you think the call later is readable? Or does it make you want to puke?
    Any idea will be greatly appreciated.
    TIA & Regards ...
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Adrian20XX
    Hi,

    I want to define a macro in C like this, but I wonder if these is too obscure and I'm starting to get too nasty.

    This is the macro I want to use:
    Code:
    #define CompareXYAndDoActions(X, Y, greaterAction, equalAction, lowerAction)			\
    {																					\
    	if (X>Y) {																		\
    		greaterAction;																\
    	}																				\
    	if (X==Y) {																		\
    		equalAction;																\
    	}																				\
    	if (X<Y) {																		\
    		lowerAction;																\
    	}																				\
    }
    Later I plan to call it like this:
    Code:
    CompareXYAndDoActions(X, Y,
    		{
    			Action1;
    		},
    		{
    			Action2;
    		},
    		{
    			Action3;
    		}
    		);
    So, do you think the call later is readable? Or does it make you want to puke?
    Any idea will be greatly appreciated.
    TIA & Regards ...
    That macro stinks, both syntactically and semantically; have a look:

    [code=c]
    if (<some_other_co ndition>)
    CompareXYAndDoA ctions(X, Y,
    {
    Action1;
    },
    {
    Action2;
    },
    {
    Action3;
    }
    ); // <--- note this semi colon
    else
    <something_else >
    [/code]

    Semantically it doesn't work either; better put parentheses around the X and Y
    macro parameters. But, even better, if you want to use Fortran you know where
    to find it.

    kind regards,

    Jos

    Comment

    • Adrian20XX
      New Member
      • Feb 2008
      • 11

      #3
      Originally posted by JosAH
      That macro stinks, both syntactically and semantically.

      Semantically it doesn't work either; better put parentheses around the X and Y macro parameters. But, even better, if you want to use Fortran you know where to find it.

      kind regards,

      Jos
      Thanks Jos,

      I thought I was getting too nasty in here, but to get that from the Chief Editor is a clear indication I've crossed the line by too much :-(

      The problem is that I had already optimized the algorithm at the higher levels, so every extra performance gain I can get in here is very important. And I wanted also to be able to decide later how to do the comparisons, I think I've read once that some Intel Processor (I guess Pentium) tests at the same time a number for greater than zero, equal than zero or less than zero, so wanted to be able to change later the conditions for (X-Y>0), (X-Y==0) and (X-Y<0) to do one comparison instead of 3.

      No other idea about how to do this? :-(

      TIA & Regards ...

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Adrian20XX
        Thanks Jos,

        I thought I was getting too nasty in here, but to get that from the Chief Editor is a clear indication I've crossed the line by too much :-(
        Yep, implementing macros that seem to add a bit of different statement syntax
        is almost never a good idea. Simply write out your statements and leave the
        optimization to the compiler (esp. the code generator): it is much better at it.

        Originally posted by Adrian20XX
        The problem is that I had already optimized the algorithm at the higher levels, so every extra performance gain I can get in here is very important. And I wanted also to be able to decide later how to do the comparisons, I think I've read once that some Intel Processor (I guess Pentium) tests at the same time a number for greater than zero, equal than zero or less than zero, so wanted to be able to change later the conditions for (X-Y>0), (X-Y==0) and (X-Y<0) to do one comparison instead of 3.

        No other idea about how to do this? :-(

        TIA & Regards ...
        Those 'micro optimizations' should only be applied (if ever) at the end of the
        development phase: first make the entire thing work using an efficient algorithm.
        As I wrote above: a compiler is a good code optimizer. If you want to use those
        very low level processor trickery-dickeries it's even better to write a (small?)
        assembly function that uses it and can be called from the rest of your code.

        Your particular example can be implemented in a 'sign' macro as follows:

        [code=c]
        #define sign(x, y) (((y) > (x)) - ((x) > (y)))
        [/code]

        This little monster has result -1, 0 or 1 according to x > y, x == y or x < y, you
        can even use that number as an index in a function pointer array if you are
        absolutely possitively definitely sure that all three situations occur frequently
        in your code; otherwise simply use a simple if statement for the comparison.

        An optimizing compiler can even break the code for that thing down to one single
        comparison and a bit of status flags (bits) fiddling.

        But as a rule of thumb: if you feel the urge to optimize: optimize late.

        kind regards,

        Jos

        Comment

        Working...