词条 | Short-circuit evaluation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
释义 |
Short-circuit evaluation, minimal evaluation, or McCarthy evaluation (after John McCarthy) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first argument of the The short-circuit expression
Short-circuit operators are, in effect, control structures rather than simple arithmetic operators, as they are not strict. In imperative language terms (notably C and C++), where side effects are important, short-circuit operators introduce a sequence point – they completely evaluate the first argument, including any side effects, before (optionally) processing the second argument. ALGOL 68 used proceduring to achieve user-defined short-circuit operators and procedures. In loosely typed languages that have more than the two truth-values
last evaluated subexpression. The expression equivalent to
x if x else y (without evaluating x twice). This is called "last value" in the table below. In languages that use lazy evaluation by default (like Haskell), all functions are effectively short-circuit, and special short-circuit operators are not needed. The use of short-circuit operators has been criticized as problematic: {{Quote|text = The conditional connectives — "cand" and "cor" for short — are ... less innocent than they might seem at first sight. For instance, cor does not distribute over cand: compare (A cand B) cor C with (A cor C) cand (B cor C); in the case ¬A ∧ C , the second expression requires B to be defined, the first one does not. Because the conditional connectives thus complicate the formal reasoning about programs, they are better avoided. |author = Edsger W. Dijkstra[1]}} Support in common programming languages
2 When overloaded, the operators && and || are eager and can return any type.3 This only applies to runtime-evaluated expressions, static if and static assert . Expressions in static initializers or manifest constants use eager evaluation.4 Fortran operators are neither short-circuit nor eager: the language specification allows the compiler to select the method for optimization. 5 ISO/IEC 10206:1990 Extended Pascal allows, but does not require, short-circuiting. 6 ISO/IEC 10206:1990 Extended Pascal supports and_then and or_else .[6]7 Smalltalk uses short-circuit semantics as long as the argument to and: is a block (e.g., false and: [Transcript show: 'Wont see me'] ).8 BASIC languages that supported CASE statements did so by using the conditional evaluation system, rather than as jump tables limited to fixed labels. 9 Delphi_(programming_language) and Free_Pascal default to short circuit evaluation. This may be changed by compiler options but does not seem to be used widely. Common useAvoiding undesired side effects of the second argumentUsual example, using a C-based language: Consider the following example: In this example, short-circuit evaluation guarantees that
Both are illustrated in the following C snippet where minimal evaluation prevents both null pointer dereference and excess memory fetches: Possible problemsUntested second condition leads to unperformed side effectDespite these benefits, minimal evaluation may cause problems for programmers who do not realize (or forget) it is happening. For example, in the code if 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.[7] Since minimal evaluation is part of an operator's semantic definition and not an (optional) optimization, many coding patterns{{Which|date=July 2010}} have come to rely on it as a succinct (if idiomatic) conditional construct. Examples include: Perl idioms: BASH (UNIX shell script) idioms[8]: Code efficiencyShort-circuiting can lead to errors in branch prediction on modern central processing units (CPUs), and dramatically reduce performance. A notable example is highly optimized ray with axis aligned box intersection code in ray tracing.{{clarify|date=November 2010}} Some compilers can detect such cases and emit faster code, but programming language semantics may constrain such optimizations.{{cn|date=October 2016}} References1. ^ Edsger W. Dijkstra "On a somewhat disappointing correspondence", EWD1009-0, 25 May 1987 full text {{DEFAULTSORT:Short-Circuit Evaluation}}2. ^ISO/IEC 9899 standard, section 6.5.13 3. ^ISO/IEC IS 14882 draft. 4. ^{{Cite web|url=https://doc.rust-lang.org/std/ops/index.html|title=std::ops - Rust|website=doc.rust-lang.org|access-date=2019-02-12}} 5. ^[https://www.etsi.org/deliver/etsi_es/201800_201899/20187301/04.10.01_60/es_20187301v041001p.pdf ETSI ES 201 873-1 V4.10.1, section 7.1.4] 6. ^{{cite web|url=http://www.gnu-pascal.de/gpc/and_005fthen.html#and_005fthen#GNU |title=and_then - The GNU Pascal Manual |publisher=Gnu-pascal.de |date= |accessdate=2013-08-24}} 7. ^{{cite web |url=http://www.itu.dk/people/sestoft/papers/SondergaardSestoft1990.pdf |title=Referential Transparency, Definiteness and Unfoldability |publisher=Itu.dk |accessdate=2013-08-24}} 8. ^{{cite web |url=https://unix.stackexchange.com/questions/190543/what-does-mean-in-bash |title=What does || mean in bash? |publisher=stackexchange.com |accessdate=2019-01-09}} 6 : Articles with example C code|Articles with example Perl code|Compiler optimizations|Conditional constructs|Evaluation strategy|Implementation of functional programming languages |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。