[Bf-committers] Style issue

Robert Wenzlaff (AB8TD) rwenzlaff at soylent-green.com
Fri Apr 14 22:55:11 CEST 2006


On Friday 14 April 2006 16:06, Alexander Ewering wrote:
> I was pretty sure that in such situations, the other two operands wouldn't
> be evaluated if the first one is already false, yes... that's why I use
> this style all over my code... and if it didn't work as I (and appearently
> the standard ;) expected, my code would be (even more) buggy ;)

If your functions are truely functions in the mathimatical sense (a set of 
inputs yields exactly one output), then it's fine.  

It's just side effects that are troublesome.   If the compiler is going to 
short-circuit the evaluation, then it should do it from left to right, but it 
is not required to short-circuit it.  So if get_res1() is false,  get_res2() 
and get_res3() may or may not get called depending on the compiler/settings.

So if some code is later relying on a side effect of get_res2() (or lack there 
of)  it will execute differently on a build that short-circuits than one that 
doesn't.

Because short-circiut evaluation is very common and it's rare to rely on 
side-effects (considered bad form by many) this rarely comes up, but it can 
cause problems.   

Because of the rarity of problems, some "highly optimized" compilers get away 
with optimizing the order of operations.   Imagine that get_res1() was 10,000 
instructions while get_res3() was only 10 instructions.  Evaluating it right 
to left gives you a 1000x performance boost.  

-- 
**********************************************
  Lesson learned from watching 'DragonballZ':
              Jedis are wimps.
**********************************************
Robert Wenzlaff    rwenzlaff at soylent-green.com



More information about the Bf-committers mailing list