In most programming languages, the arguments passed to a function are evaluated before the function uses them, that is, they are evaluated impatiently.
It seems to me much more reasonable for me to evaluate the arguments only after the function uses them, i.e. lazily. This makes more sense to me, because it seems that he will have a performance advantage: why evaluate things that are never needed?
Also, suppose you wanted to implement an if function that takes a boolean, and an object to return if the boolean is true, and another object to return if the boolean is false:
object if(bool condition, object valueIfTrue, object valueIfFalse) { if(condition) return valueIfTrue; return valueIfFalse; }
In a language that readily evaluates arguments, both objects are always evaluated, although a function will always need only one of them, which in the best case incurs minor unnecessary overhead and, in the worst case, causes an infinite loop.
However, since most programming languages use an impatient evaluation of function arguments, I suppose there must be a reason why this is usually done this way. Is there any great benefit from an impatient assessment here that I am missing from view simply because it was easier to implement languages in this way, is it just a tradition or what?
source share