Unnecessary Macros (was Re: Unnecessary Parenthesis)

Alexis Dimitriadis alexis at reed.UUCP
Sun Oct 23 08:35:23 AEST 1988


Whoever claimed to NEVER write macros that do not behave just like functions
(i.e., use their argument more than once, modify an argument, or reference
local variables) has good reason for doing so.  Still, features
unique to macros are sometimes the most convenient way to get from here to
there.  

Yes, there are pitfalls that should be scrupulously avoided.  
If my macro-with-arguments might not behave like a function, I make sure 
it does not _look_ like one:  I spell its name in uppercase:

#define MAX(x, y) (((x) > (y)) ? (x) : (y))

Am I naive in expecting any programmer to check the definition before using
something named MAX() with a "dangerous" argument like ++i?  Probably...

BTW, I nevertheless feel that using a separately-defined local variable is
BAD form.  If I need an extra variable, I restrict its scope:

#define SWAP(x,y) do{ long tmp = x; x = y; y = tmp; } while (0)

You cannot do that with a function! (Of course, you cannot use the above in
an expression...)

Thank you for reading through my $0.02 worth; and forgive me if I just
brought up something that got thrashed to death three months ago.

Alexis Dimitriadis
tektronix!reed!alexis



More information about the Comp.lang.c mailing list