Code like this:
is more legible as follows:
The return statement (Section 20.3.3) is actually costly, because it is also in charge of stopping all the event-handlers, detached code, and code sent into background (via ‘,’ or ‘&’).
In a large number of cases, return is actually useless. For instance instead of:
function inBounds1(var x, var low, var high)
{
if (x < low)
return false;
if (high < x)
return false;
return true;
}|;
assert
{
inBounds1(1, 0, 2); inBounds1(0, 0, 2); inBounds1(2, 0, 2);
!inBounds1(0, 1, 2); !inBounds1(3, 0, 2);
};
write
function inBounds2(var x, var low, var high)
{
if (x < low)
false
else if (high < x)
false
else
true;
}|;
assert
{
inBounds2(1, 0, 2); inBounds2(0, 0, 2); inBounds2(2, 0, 2);
!inBounds2(0, 1, 2); !inBounds2(3, 0, 2);
};
or better yet, simply evaluate the Boolean expression. As a matter of fact, returning a Boolean is often the sign that you ought to “return” a Boolean expression, rather than evaluating that Boolean value using a conditional, and return either true or false.