Why is lazy evaluation useful?

后端 未结 22 1614
无人共我
无人共我 2020-11-29 17:04

I have long been wondering why lazy evaluation is useful. I have yet to have anyone explain to me in a way that makes sense; mostly it ends up boiling down to \"trust me\".<

22条回答
  •  暖寄归人
    2020-11-29 17:43

    Consider this:

    if (conditionOne && conditionTwo) {
      doSomething();
    }
    

    The method doSomething() will be executed only if conditionOne is true and conditionTwo is true. In the case where conditionOne is false, why do you need to compute the result of the conditionTwo? The evaluation of conditionTwo will be a waste of time in this case, especially if your condition is the result of some method process.

    That's one example of the lazy evaluation interest...

提交回复
热议问题