Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Short-circuit evaluation
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Possible problems== === Untested second condition leads to unperformed side effect === Despite these benefits, minimal evaluation may cause problems for programmers who do not realize (or forget) it is happening. For example, in the code <syntaxhighlight lang="c"> if (expressionA && myfunc(b)) { do_something(); } </syntaxhighlight> if <code>myfunc(b)</code> is supposed to perform some required operation regardless of whether <code>do_something()</code> is executed, such as allocating system resources, and <code>expressionA</code> evaluates as false, then <code>myfunc(b)</code> will not execute, which could cause problems. Some programming languages, such as [[Java (programming language)|Java]], have two operators, one that employs minimal evaluation and one that does not, to avoid this problem. Problems with unperformed side effect statements can be easily solved with proper programming style, i.e., not using side effects in boolean statements, as using values with side effects in evaluations tends to generally make the code opaque and error-prone.<ref>{{cite web |url=http://www.itu.dk/people/sestoft/papers/SondergaardSestoft1990.pdf |title=Referential Transparency, Definiteness and Unfoldability |publisher=Itu.dk |access-date=2013-08-24}}</ref> ===Reduced efficiency due to constraining optimizations=== Short-circuiting can lead to errors in [[branch prediction]] on modern [[central processing unit]]s (CPUs), and dramatically reduce performance. A notable example is highly optimized ray with axis aligned box intersection code in [[Ray tracing (physics)|ray tracing]].{{clarify|date=November 2010}} Some compilers can detect such cases and emit faster code, but programming language semantics may constrain such optimizations.{{citation needed|date=October 2016}} An example of a compiler unable to optimize for such a case is [[Java (programming language)|Java]]'s [[HotSpot (virtual machine)|Hotspot virtual machine]] (VM) as of 2012.<ref>{{cite web |last=Wasserman |first=Louis |date=11 July 2012 |title=Java: What are the cases in which it is better to use unconditional AND (& instead of &&) |url=https://stackoverflow.com/a/11412121 |website=Stack Overflow}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)