Is it a good idea to implement business logic functions as macros?
I inherited some legacy C++ code and I find a whole lot of business logic functions are implemented as long cryptic macros.
Is there an advantage of macros over functions? What is the general rationale behind the use of macros?
What kind of logic is best suited for macros?
Here is a simple sample from code
#define INSERT_VALUES(IN,ID,EO) {\
double evaluationOutput = EO;\
int controls = 0;\
int input_controls = m_input_controls[IN];\
if(m_value_list[IN].ShouldProcess())\
{\
evaluationOutput = m_evaluationOutput[IN];\
controls = m_controls[IN];\
}\
VALUE_EXIST(evaluationOutput,controls,input_controls,IN,ID,Adj);\
m_evaluationOutput[IN] = controls > 0 ? evaluationOutput : 0.0;\
m_controls[IN] = controls;\
m_input_controls[IN] = input_controls;\
}
In Effective C++, Scott Meyers notes in Item 2:
Prefer
consts
,enums
, andinlines
to#defines
Specifically referring to the practice of writing macros instead of functions, Meyers says:
Another common (mis)use of the
#define
directive is using it to implement macros that look like functions but don't incur the overhead of a function call.Macros like this have so many drawbacks, just thinking about them is painful.
Fortunately, you don't have to put up with this nonsense. You can get all the efficiency of a macro plus all the predictable behavior and type safety of a regular function by using a template for an inline function.
[Real functions] obey scope and access rules. For example, it makes perfect sense to talk about an inline function that is private to a class. In general, there's just no way to do that with a macro.
To answer your questions specifically:
#include
and (maybe) #ifdef
/#ifndef
for conditional compilation. As Meyers notes in closing:
It's not yet time to retire the preprocessor, but you should definitely give it long and frequent vacations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With