Take the 2-minute tour ×

I am having a discussion with a colleague about using macro as a thin (extremely) layer of abstraction vs using a function wrapper. The example that I used is

Macro way.

#define StartOSTimer(period)  (microTimerStart(period))

Function wrapper method

void StartOSTimer(period)

Personally, I liked the second method as it allows for future modification, the #include dependencies are also abstracted as well.

share|improve this question
Your colleague probably thinks that somehow your version is slower. If it is, the time difference will probably unnoticeable and anyway, one can expect any decent compiler to inline the call completely where possible. Use the second method. –  ereOn Mar 29 '12 at 11:20
There's nothing wrong with macros. Check out SGLIB. If misused, macros can usually do more damage than functions, but improper use of functions alone is enough to write crappy code. Look at the code (or even better: have someone look at it) and if it's self-explanatory you've done it right. Intelligent use of macros can in fact decrease noise and thereby increase clarity. –  back2dos Dec 4 '12 at 12:18
add comment

2 Answers

Macros are hideous abominations that should be avoided in every situation you can possibly get away with. You'd have to have a fairly extreme justification to use one.

share|improve this answer
Take a look at LLVM and Clang source code for an example of smart, clean and maintainable use of the preprocessor. Then try to think of how to do the same thing without it. –  SK-logic Mar 29 '12 at 10:15
@SK-logic: Because they have such small codebases, I can find a suitable example easily. –  DeadMG Mar 29 '12 at 10:58
@SK-logic: Do note that DeadMG said "You'd have to have a fairly extreme justification to use one." There are times where macros are needed. The OP's case is not one of those times. –  In silico Mar 29 '12 at 11:15
@DeadMG, have you ever heard of grep? Look for sequences of #define something and then #include "whatever.inc". –  SK-logic Mar 29 '12 at 12:50
add comment

Since you use wrapper, you can also use it with a function pointer. If you use it as such, you can pass a stub function, and unit test functions that uses it. You can not do it with macro.

share|improve this answer
add comment

Your Answer


