Unnecessary Macros (was Re: Unnecessary Parenthesis)

Dave Jones djones at megatest.UUCP
Tue Oct 4 10:19:56 AEST 1988


>From article <701 at accelerator.eng.ohio-state.edu), by rob at 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".)



More information about the Comp.lang.c mailing list