Unnecessary Macros (was Re: Unnecessary Parenthesis)

Physically Pffft jlh at loral.UUCP
Wed Sep 28 02:43:01 AEST 1988


I was sure someone else would mention this so I didn't keep a copy of the
original article to quote.  In general, someone is making a macro
to do z = x*x + y*y, but using a variable 'temp' to do it.  I was under the
impression this was a Very Bad Idea for 2 reasons.  Lets assume Mr C Wizard
has left the company 6 months ago and now it's up to Miss Molly to change
the code.  First, she could use this power macro in a function that didn't
have the variable temp defined.  No problem, the compiler spits out the
undefined variable.  On the other hand, suppose she wants to put this macro
into a loop.  She says to herself 'self, here's a variable temp that ain't used
here'.  So we get

	for(temp = 0; temp < wanna_quit; temp++){
		z = power(x,y);
		some_other_stuff;
	}

Well boys and girls, lets just hope this blows up in a way thats immediatly
obvious.  As a third scenario, combine the first two scenarios.  Poor Miss
Molly adds the power macro to a function that already has temp defined
and is definately needed.  And lets say it's used in such a way that
her changes work fine and dandy, after all, she doesn't use temp in any
of her changes, but changes the original code in some insidious way.
Again, lets hope the result of changing temp is immediatly obvious.

The moral?  Well, if you have to use a temp variable in a macro then
PASS the bloody thing.

#define power(x,y, tmp) (tmp = x*x + y*y, x = tmp)

result = power(x, y, temp);

This ensures that the poor overworked sucker who ends up maintaining your
code knows damn well that your macro requires a temp variable to work right.


							Jim



-- 
Jim Harkins 
Loral Instrumentation, San Diego
{ucbvax, ittvax!dcdwest, akgua, decvax, ihnp4}!ucsd!sdcc6!loral!jlh



More information about the Comp.lang.c mailing list