Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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)
{
    microTimerStart(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 2

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
1  
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
1  
@SK-logic: Because they have such small codebases, I can find a suitable example easily. –  DeadMG Mar 29 '12 at 10:58
1  
@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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.