Questions tagged [short-circuiting]

A feature of some languages to skip certain code at runtime that doesn't affect the outcome, especially when testing compound conditions

When executing code, the result of evaluating a compound condition - one that is comprised of multiple logical parts, eg A OR (B AND C) - may be known before all parts are known.

For example, if it is known that A is true, the result of evaluating A OR B is true regardless of the value of B. In languages the support it, eg Java, the runtime environment can short circuit the evaluation at some point earlier than the end if the runtime can determine that the overall result will not be affected by any following evaluations within the compound condition.

Generally speaking, short circuits may be made in one of two conditions:

  1. A OR B when A is true (the result is always true)
  2. A AND B when A is false (the result is always false)

This feature gives performance benefits and runtime safety.

Performance

If the second part of the compound condition is expensive to execute, but only relevant if some other condition applies, the expense can be avoided when appropriate. Consider this (java) code:

if (orderObject.isConfirmed() && orderObject.calculateShippingRouteLength() > 1000) {
    display("Your address is too far away");
    return;
}

The expensive call to calculateShippingRouteLength() is avoided unless the order is confirmed.

Safety

Consider what would happen if this java condition was not short circuited:

if (string != null && string.contains("foo"))

If string is null and the evaluation was not short circuited, a NullPointerException would be thrown when the second half was evaluated.

466 questions
493
votes
5 answers

Is there a conditional ternary operator in VB.NET?

In Perl (and other languages) a conditional ternary operator can be expressed like this: my $foo = $bar == $buz ? $cat : $dog; Is there a similar operator in VB.NET?
Jim Counts
  • 12,535
  • 9
  • 45
  • 63
451
votes
3 answers

Does Python support short-circuiting?

Does Python support short-circuiting in boolean expressions?
Dinah
  • 52,922
  • 30
  • 133
  • 149
307
votes
8 answers

How to check for null in Twig?

What construct should I use to check whether a value is NULL in a Twig template?
Fluffy
  • 27,504
  • 41
  • 151
  • 234
283
votes
13 answers

What is the difference between And and AndAlso in VB.NET?

In VB.NET, what is the difference between And and AndAlso? Which should I use?
Nakul Chaudhary
  • 25,572
  • 15
  • 44
  • 47
173
votes
7 answers

Is short-circuiting logical operators mandated? And evaluation order?

Does the ANSI standard mandate the logical operators to be short-circuited, in either C or C++? I'm confused for I recall the K&R book saying your code shouldn't depend on these operations being short circuited, for they may not. Could someone…
Joe Pineda
  • 5,521
  • 3
  • 31
  • 40
166
votes
15 answers

Is the SQL WHERE clause short-circuit evaluated?

Are boolean expressions in SQL WHERE clauses short-circuit evaluated ? For example: SELECT * FROM Table t WHERE @key IS NULL OR (@key IS NOT NULL AND @key = t.Key) If @key IS NULL evaluates to true, is @key IS NOT NULL AND @key = t.Key…
Greg Dean
  • 29,221
  • 14
  • 67
  • 78
158
votes
10 answers

React showing 0 instead of nothing with short-circuit (&&) conditional component

I have the following simple short-circuit statement that should show either a component or nothing: {profileTypesLoading && } If the statement is false, it renders a 0 instead of nothing. I have done a…
Gurnzbot
  • 3,742
  • 7
  • 36
  • 55
145
votes
3 answers

Does JavaScript have "Short-circuit" evaluation?

I would like to know if JavaScript has "short-circuit" evaluation like &&-operator in C#. If not, I would like to know if there is a workaround that makes sense to adopt.
GibboK
  • 71,848
  • 143
  • 435
  • 658
141
votes
9 answers

Is there actually a reason why overloaded && and || don't short circuit?

The short circuiting behaviour of the operators && and || is an amazing tool for programmers. But why do they lose this behaviour when overloaded? I understand that operators are merely syntactic sugar for functions but the operators for bool have…
111
votes
2 answers

Ternary operator in Java only evaluating one expression since Java 7 - was that different in Java 1.6 and lower?

Preparing for the Oracle Certified Associate Java SE 8 Programmer 1 exam, I came across the following paragraph about the ternary expression in the official Study Guide: Ternary Expression Evaluation As of Java 7, only one of the right-hand…
Mathias Bader
  • 3,585
  • 7
  • 39
  • 61
110
votes
7 answers

What's the difference between & and && in MATLAB?

What is the difference between the & and && logical operators in MATLAB?
Fantomas
  • 1,495
  • 4
  • 12
  • 21
103
votes
4 answers

In Twig, check if a specific key of an array exists

In PHP we can check if a key exists in an array by using the function array_key_exists(). In the Twig templating language we can check if an variable or an object's property exists simply by using an if statement, like this: {% if app.user %} do…
user852610
  • 2,205
  • 7
  • 36
  • 46
93
votes
12 answers

Why doesn't Java have compound assignment versions of the conditional-and and conditional-or operators? (&&=, ||=)

So for binary operators on booleans, Java has &, |, ^, && and ||. Let's summarize what they do briefly here: JLS 15.22.2 Boolean Logical Operators &, ^, and | JLS 15.23 Conditional-And Operator && JLS 15.24 Conditional-Or Operator || For &, the…
92
votes
10 answers

if statement - short circuit evaluation vs readability

Sometimes, an if statement can be rather complicated or long, so for the sake of readability it is better to extract complicated calls before the if. e.g. this: if (SomeComplicatedFunctionCall() || OtherComplicatedFunctionCall()) { // do…
relaxxx
  • 7,566
  • 8
  • 37
  • 64
85
votes
8 answers

Does PHP have short-circuit evaluation?

Given the following code: if (is_valid($string) && up_to_length($string) && file_exists($file)) { ...... } If is_valid($string) returns false, does the php interpreter still check later conditions, like up_to_length($string)? If so, then why…
Muntasir
  • 1,130
  • 1
  • 8
  • 9
1
2 3
31 32