473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. #define CompareXYAndDoActions(X, Y, greaterAction, equalAction, lowerAction)            \
  2. {                                                                                    \
  3.     if (X>Y) {                                                                        \
  4.         greaterAction;                                                                \
  5.     }                                                                                \
  6.     if (X==Y) {                                                                        \
  7.         equalAction;                                                                \
  8.     }                                                                                \
  9.     if (X<Y) {                                                                        \
  10.         lowerAction;                                                                \
  11.     }                                                                                \
  12. }
Later I plan to call it like this:
Expand|Select|Wrap|Line Numbers
  1. CompareXYAndDoActions(X, Y,
  2.         {
  3.             Action1;
  4.         },
  5.         {
  6.             Action2;
  7.         },
  8.         {
  9.             Action3;
  10.         }
  11.         );
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 ...
Apr 27 '08 #1
3 1722
JosAH
11,448 Expert 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. #define CompareXYAndDoActions(X, Y, greaterAction, equalAction, lowerAction)            \
  2. {                                                                                    \
  3.     if (X>Y) {                                                                        \
  4.         greaterAction;                                                                \
  5.     }                                                                                \
  6.     if (X==Y) {                                                                        \
  7.         equalAction;                                                                \
  8.     }                                                                                \
  9.     if (X<Y) {                                                                        \
  10.         lowerAction;                                                                \
  11.     }                                                                                \
  12. }
Later I plan to call it like this:
Expand|Select|Wrap|Line Numbers
  1. CompareXYAndDoActions(X, Y,
  2.         {
  3.             Action1;
  4.         },
  5.         {
  6.             Action2;
  7.         },
  8.         {
  9.             Action3;
  10.         }
  11.         );
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:

Expand|Select|Wrap|Line Numbers
  1. if (<some_other_condition>)
  2.    CompareXYAndDoActions(X, Y,
  3.         {
  4.             Action1;
  5.         },
  6.         {
  7.             Action2;
  8.         },
  9.         {
  10.             Action3;
  11.         }
  12.         ); // <--- note this semi colon
  13. else
  14.    <something_else>
  15.  
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
Apr 27 '08 #2
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 ...
Apr 27 '08 #3
JosAH
11,448 Expert 8TB
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.

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:

Expand|Select|Wrap|Line Numbers
  1. #define sign(x, y) (((y) > (x)) - ((x) > (y)))
  2.  
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
Apr 27 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: qwweeeit | last post by:
Hi all, when I moved from Windows to Linux I choosed Python as my language of reference and as GUI, Qt (not much investigated up to now). Till now I didn't regret but one thing: Python can't act...
3
by: arut | last post by:
I would like to know when a const should be used and when a #define is necessary in a program using a constant. What are the pros and cons?
3
by: Andrew | last post by:
Hello, Is it bad practice to use a function as a parameter of a macro? Will the compiler create a full copy of the function's machine code for each invocation of the macro? Or does it create...
8
by: sathyashrayan | last post by:
/* ** PLURALTX.C - How to print proper plurals ** public domain - original algorithm by Bob Stout */ #include <stdio.h> #define plural_text(n) &"s"
5
by: krbyxtrm | last post by:
Hi is there a way to 'manage' function execution using macros? #define MY_CALL_MACRO(MacroName) { g_MacroStack.push_back(MacroName); <some code here...>} such that when i use the...
17
by: Francois Grieu | last post by:
Consider this macro // check if x, assumed of type unsigned char, is in range #define ISVALID(x) ((x)>=0x20 && (x)<=0x7E) Of course, this can't be safely used as in if (ISVALID(*p++)) foo();...
3
by: NickP | last post by:
Hi there, I am trying to do a reflection only load of an assembly, within a new appdomain, within a macro. i.e. create new app domain instantiate class run class
2
by: talkaboutquality | last post by:
Need to define a macro as, say, #ifdef NEED_FUNCTION foo(x) #else #endif
1
by: todWulff | last post by:
Good day folks. Let me open with the statement that I am not a C++/C programmer. The environment that I am programming in is ARMbasic, an embedded BASIC targeted toward ARM-based...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.