4 ms·
Returning multiple times can also reduce nesting, since you can add guard-like statements and return early. Take this contrived example: function foo(bar
by Someone1234 8y ago
Returning multiple times can also reduce nesting, since you can add guard-like statements and return early.
Take this contrived example:
function foo(bar)
{
var result, error;
if(bar is null)
{
error = "{NameOf(bar)} is required";
result = null;
}
if(bar is not null)
{
... Main function logic ...
}
return Tuple(result, error);
}
If you can return multiple times instead you'd be able to write:
function foo(bar)
{
var result, error;
if(bar is null)
{
return Tuple(null, "{NameOf(bar)} is required");
}
... Main function logic ...
return Tuple(result, error);
}
Assume "Main function logic" has several of nest of its own (branching, loops, etc) it quickly gets difficult to read.
- deleted 8y ago[deleted]