Navigation
  • Home
  • Recent
  • Most Active
  • Popular
  • Blog
  • Credits
  • RSS
  •   Interaction
  • Register
  • Statistics
  •   Help
  • Suggestions
  • Contact Us
  • How to Edit
  • Help



  • [Edit]


    Minimal evaluation or short circuit evaluation is an evaluation strategy in which an expression is only evaluated until the point where its final value is known. In some cases, it is not necessary to evaluate all the parts of an expression. Consider the following example using the C programming language:

    int a = 0;
    if (a && myfunc(b))


    In this example minimal evaluation guarantees that myfunc(b) is never called. This is because a evaluates to false, and false AND q evaluates to false for any value of q. This feature permits two useful programming constructs. Firstly, if the first sub-expression checks whether an expensive computation is needed and the check evaluates to false, one can eliminate expensive computation in the second argument. Secondly, it permits a construct where the first expression guarantees a condition without which the second expression may cause a runtime error, such as in the following C code where minimal evaluation prevents a null pointer dereference:

    int is_three_chars_long(const char
      p)

    Despite this benefits, minimal evaluation may cause problems for programmers who do not realize (or forget) it is happening. For example, in the code

    if (a && myfunc(b))

    if myfunc(b) is supposed to perform some required operation, such as allocating system resources, and a evaluates as false, then myfunc(b) will not execute, which could cause problems. Some programming languages, such as Java, have two operators, one that employs minimal evaluation and one that does not, to avoid this problem.

    It is worth noting that these expressions may be considered a more compact way of expressing a nested branch or sequence of branches. For example:

    if (cond_a && expensive_or_dangerous_cond_b) else

    becomes (through logical and expansion)

    if (cond_a) else


    If code duplication is an issue, a more involved translation stores the result of the decision in a variable, as in:

    int branch;
    if (cond_a) else
    if (branch == 0) else

    A compiler using the common subexpression elimination and constant propagation optimizations might generate the same code for all three versions.

    Minimal evaluation can also be used to conditionally execute some code for side effects, as in the common Perl idioms:

    some_condition or die;
      Abort execution if some_condition is false
    some_condition and die;
      Abort execution if some_condition is true



        Minimal evaluation
            See also

    top

    See also



     
    Search more:
     

       
    Source Privacy License Download Contact Us Atlas
    Scientus.org Dictionary (Yet Another Wiki) RC : 1.39
    MIT OpenCourseWare
    This article is licensed under the GNU Free Documentation License [copyleft]. It uses material from the Wikipedia article "Minimal evaluation". link