I’ve been looking through a fair amount of code lately where the author has
elected to use return
with multiple values. For those who haven’t seen this
witchery, it looks something like this:
|
I am not a big fan of this and try to avoid it when I write code. To me it
hides what is really happening, which is the fact that an array is being
constructed that has heavy importance on the order of each value. It also
forces you to use the return
keyword, which I prefer not to have at all
when I can avoid it. (+10 for Erlang not having return
)
The same method can be rewritten as follows:
|
This, to me doesn’t feel to much better. It has to do with the fact we have created an ad-hoc datum object that will leave future readers slightly puzzled. After giving it some thought here is what I landed on for a solution I personally like.
|
“Why did I extend Array
?”, you may ask. In this case only to be compatible
with why the original multiple return was even a thing to begin with.
|
I also recommend avoiding this when you can as well. This ends my small rant on multiple returns… I urge everyone to avoid using them and instead realize there is a new data structure that is begging to be named.