Tuesday, September 17, 2013

two men enter, one man leaves (blast from the past)

(Just found something I wrote in 2004...) You know, I just thought of one major lack of Java, and to be fair, most C-derived programming languages: a function can have many input parameters but only one return value. That's a really odd asymmetry to have to put up with.

Perl, for example, handles it much better...it's no problem to write something like

($foo, $bar, $baz) = somefunction($a,$b,$c);

In Java, though, you'd have two ugly workarounds: create a wrapper object that contains each thing you want to return, or if the objects are complex, sometimes you can have the calling program create an object, pass it in, and let the called function fill in the blanks. (Kind of like Oracle's "INOUT" parameters.) (Followup: since I wrote this in 2004, JSON has gained in popularity and influenced other languages too, so that returning or passing around a Map of key/value pairs is probably more acceptable than it used to be.)

Personally, I think this lack is something that provokes over use of Exceptions, which really do horrendous things to understanding a programs flow of execution.

Seriously, would it be so hard to add a syntax so something like this would work?

int foo;String bar;(foo,bar,String baz) = somefunction();

public int,String,String somefunction(){ return (5,"hey","ho");}

Does anyone know if any of the other C-derived languages handle this case better? C# or any of that? I know they're hyper-conservative about adding this kind of structure to Java, since it breaks old compilers and what not. But still, it seems like one of those things that actually is pretty stupid and only around for legacy reasons but that everyone just kind of accepts.