Path: utzoo!attcan!uunet!seismo!esosun!ucsdhub!loral!jlh From: jlh@loral.UUCP (Physically Pffft) Newsgroups: comp.lang.c Subject: Re: Unnecessary Macros (was Re: Unnecessary Parenthesis) Message-ID: <1851@loral.UUCP> Date: 27 Sep 88 16:43:01 GMT References: <2089@ssc-vax.UUCP> <441@kaon.uchicago.edu> <1401@devsys.oakhill.UUCP> <23@datcon.UUCP> <8577@smoke.ARPA> <8078@haddock.ima.isc.com> <70279@sun.uucp> Reply-To: jlh@loral.UUCP (Physically Phffft) Organization: At the Remote Controls Lines: 43 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