Archived

This topic is now archived and is closed to further replies.

Using the && operator as a short-curcuit

This topic is 5587 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I thought it was possible to replace: if(x){doSomething(x);} with something like: x && doSomething(x); but my compiler is telling me "void value not ignored as it ought to be". What''s the proper syntax for the second expression above? I tried putting paranthesis around it but that didn''t help. Sean

Share this post


Link to post
Share on other sites
no, you replace things like


  
if(x)
doSomething(x);

// with


if(x && doSomething(x))
;
//or


if(x)
if(doSomething(x))
doSomthingElse(x);

// with


if(x && doSomething(x)
doSomethingElse(x);


using a C behavior called short-circuit evaluation, which means that the C compiler evaluates the elements of a compound logical expression from left-to-right, and stops evaluating at the first case where the final outcome becomes known...

the main place this is used is so you can do this type of thing:


  
if(x != NULL && x->DoSomething())
// do succeeded stuff here

else
// do failure stuff here ...



this way you don''t need to nest the NULL check and the actual function call into extra levels ... cause the function call will never happen if the null check fails.

Share this post


Link to post
Share on other sites
I may be incorrect, but I believe that the order of evaluation is not garaunteed so you might want to be careful about using it like you want to.

Karg

Share this post


Link to post
Share on other sites
quote:
Original post by Karg
I may be incorrect, but I believe that the order of evaluation is not garaunteed so you might want to be careful about using it like you want to.

Karg


It is guaranteed to be left-to-right for &&, ||, the comma operator, and ?:. i.e. they have sequence points inserted.


Share this post


Link to post
Share on other sites
Xai, the replacement I presented in my original post does work (as long as doSomething returns non-NULL). The short-circuiting behavior of && is not limited to "if" expressions.

(Chances are this will already be pointed out by the time I''m done collecting 500 errors.)

Sean

Share this post


Link to post
Share on other sites
I would suggest not using the "short circuit". It obfuscates the intent of the code, and makes it less readable (wait.. that''s redundant... oh well)

Share this post


Link to post
Share on other sites
it is perfectly comon and readable to do a test for NULL (0) on the same line as the use of the newly verified non-null pointer. Most other uses of short-circuit evaluation ARE strange ... especially when you see stuff like this.

DoOriginalJob() || DoThisIfFirstJobFailed()

very hard to understand that a true from job 1 stops evaluation

and this

DoJobOne() && DoNextJob() && DoFinalJob()

it''s really not so obviious that the final job happen ONLY if both other jobs return true ...

Share this post


Link to post
Share on other sites
It just takes a full understanding of how those operators are handled really.

This is the way I remember it, and it''s quite a nice way of remembering it as well. With Logical OR, statements are executed until TRUE is found. With Logical AND, statements are executed until FALSE is found. Of course, if the Logical OR never finds that TRUE value, then the expression evaluates to FALSE, and vice versa if Logical AND never finds that FALSE value.

Share this post


Link to post
Share on other sites
I don''t really see the problem with using structures like this in code.. They aren''t confusing once you''ve seen them a few times. The problem is when you use this kind of thing because you think it will make your code faster.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
ok, why exactly would this make the code faster? Don''t they both get changed into conditional jump instructions?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
it wont make anything faster and has the potential to make it slower and harder to read. newbs seem to have the idea in their heads that crytic = speed, l33t, efficient. even though its not the case

Share this post


Link to post
Share on other sites
I think it makes it easier to understand, because there''s less information to digest. If it''s a null check, it makes sesn to put it all in one conditional; they are one logical operation, they belong together.

And it''s a common technique, and it''s *important* that you know that C++ defaults to short-curcuit evaluation - so anyway you dice it, you need to know it.


quote:
Qoy
The problem is when you use this kind of thing because you think it will make your code faster.


Why would you think that?

Share this post


Link to post
Share on other sites