I have heard of the term "short-circuiting" being used in C, C++, C#, Java, and many others. What does this mean and in what scenario would it be used?
3 Answers
Short circuiting in C is when a logical operator doesn't evaluate all its arguments.
Take for example and &&
, it's pretty obvious that 0 && WhoCares
is going to be false no matter what WhoCares
is. Because of this, C just skips evaluating WhoCares
. Same goes for 1 || WhoCares
, it'll always be true. Because of this, we can write code like
CanFireMissiles && FireMissiles()
This way we avoid doing some potentially impossible operation. If we can't fire the missiles we certainly don't want to try to. This is commonly used with pointers, especially file pointers.
bool isN(int* ptr, int n){
return ptr && *ptr == n;
}
This plays out in lots of other useful ways to avoid unnecessary computing
isFileReady() || getFileReady()
This avoids doing extra work if we don't need to.
-
2Anytime, if I've answered your question you can check the checkbox beside it to mark your question as answered Jun 18, 2013 at 14:16
-
8I don't love
CanFireMissiles && FireMissiles()
, as it makes me suspect you're abusing short-circuiting to trigger side effects. I feel like you're hiding actions in a conditional. Such code is better written asif(CanFireMissiles){FireMissiles();}
orif(CanFireMissles){didFireMissiles = TryFireMissiles(); if(didFireMissiles){...}}
.– BrianJun 18, 2013 at 20:51 -
2I'd argue that the only use is to hide side effects. Usually not the the "Blowing up a city" sort but things like dereferencing a pointer or using system resources are also done in this manner in C quite often. See the wikipedia page, the whole section under usage is "Hiding side effects" Jun 18, 2013 at 20:58
-
3@jozefg you can also use it to prevent doing expensive operations like
IsInCache(value) || IsInDatabase(value)
, where IsInDatabase might take time (especially if using a mobile device and network latency is an issue).– mgw854Nov 12, 2015 at 18:00
"Short Circuiting" typically refers to "Short Circuit Evaluation" which is a general concept, not just C specific.
Boolean operators evaluation left to right, so any terms that will render the other terms unnecessary are useful. So you might check for a condition that excludes other conditions later on, thus allowing a partial evaluation of the logical operations rather than evaluating the whole thing.
Example:
while((x && y) == 1) {
//This bit will not execute if x is 0 or y is 0 but y won't even be
//evaluated due to short circuit evaluation if x is 0.
}
A more complex example:
if((a || b || c || d || e || f || g || h || i || j || k) == 1) {
/* If any of these are equal to 1 the whole expression is equal to 1,
* thus doesn't it make sense to short circuit evaluate this?
* Saves a bunch of time.
*/
}
-
8Short circuiting is less about saving time but more about not being evaluated. A function not being evaluated will also not have the side effect would it be evaluated.– Pieter BJun 18, 2013 at 14:14
-
You know, the
== 0
is not only unneccessary, it might actually confuse some people. Nov 13, 2015 at 3:55
Short ciruit evaluation can lead to some parts of a condition not be evaluated.
For example:
if (true || f()) { ... }
will not exectue f
.
&&
operator.||
as well... at least it should.||
and&&
to&
and|
to see the subtle difference. Have a simple program evaluate1 || printf("yay");
vs0 || printf("yay");
and1 | printf("yay");
vs0 | printf("yay");
to see the differeces