a style question

Stanley Friesen sarima at tdatirv.UUCP
Sat Oct 6 09:14:25 AEST 1990


In article <1275.270afcdb at iccgcc.decnet.ab.com> browns at iccgcc.decnet.ab.com (Stan Brown, Oak Road Systems) writes:
>davis at pacific.mps.ohio-state.edu  (John E. Davis) writes:
>> Which ['x != 100' or 'x < 100' RHS] generates faster code?
 
>When hesitating between two styles, I believe that _in general_ these
>questions are the ones to ask, in descending order of priority:
>
>    1. Which is correct?  ...
>    2. Which is clearer? ...
>    3. Which is faster?

I think you missed an important question:

	1.5 Which is more robust?  - that is which form is less likely to
	    lead to a bug being introduced later?

This is *not* the same as question #1, since both can be correct *now*
and still differ as to roubustness.  In this particular case this is
the situation.  'x != 100' used in a loop (as the original question asked)
is *dangerous*.  If the loop should happen to increment x somehere other
than the loop control, the resulting loop will likely be infinite!
The 'x < 100' form guarentees that no matter how the loop is modified later
it will terminate correctly, rather than hang the program (or worse).
Have pity on the maintainer and use the robust form!
-- 
---------------
uunet!tdatirv!sarima				(Stanley Friesen)



More information about the Comp.lang.c mailing list