Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!purdue!decwrl!megatest!djones From: djones@megatest.UUCP (Dave Jones) Newsgroups: comp.lang.c Subject: Re: Unnecessary Macros (was Re: Unnecessary Parenthesis) Message-ID: <855@goofy.megatest.UUCP> Date: 4 Oct 88 00:19:56 GMT References: <701@accelerator.eng.ohio-state.edu) Organization: Megatest Corporation, San Jose, Ca Lines: 34 From article <701@accelerator.eng.ohio-state.edu), by rob@kaa.eng.ohio-state.edu (Rob Carriere): ) ) How about the following, deep in some inner loop: ) ) foo = square( sin( x )) + 1.7; ) ) I *don't* want to write: ) ) foo = sin( x )*sin( x ) + 1.7; ) ) and ) ) temp = sin( x ); ) foo = temp*temp + 1.7; ) ) is clearly less legible. ^^^^^^^ ^^^^ ^^^^^^^ To me it is clearly much more legible, becuase I don't have to worry about whether "square" has side-effects! I would write it like this: { register double sin_x = sin(x); foo = (sin_x * sin_x) + 1.7; } Always restrict the scope of variables as strictly as possible. In the above, the reader (and the compiler!) knows that the variable sin_x is never used except on those two lines. (If the compiler were smart enough to know that sin() has no side-effects, it could transform " foo = sin(x)*sin(x) + 1.7 " into the code I prefer by means of "common subexpression removal".)