Wednesday, August 15, 2007

Named Arguments - Pure Danger (Tech) for Java 7?

On his blog, Alex Miller is has put up Some comments on D&R’s Java 7 wishlist. One is about Named Arguments:

I have seen no one propose this, but I’ve been thinking for a while that it would be really helpful in situations where you have lots of optional parameters. It would be great for constructor injection, argument passing on a program interface, embedded DSLs/fluent interfaces, etc.


I really like the idea of named arguments, although I'm not sure if they are the best solution for all the things mentioned above: DSLs in Java? Without operator overloading?.


Anyway, it believe they would make Java Code (even more) readable and the usage could prevent mistakes:



Documentation and parameter re-ordering are nice, but it would be really cool to have default values along with them:



However, when does it end? Imagine indexed varargs:



And did I hear critics say "But named methods can simulated with a dedicated bean (having a corresponding property for each method parameter)!"
Well, that would leads to lots of grabage of code (and objects inside the jvm):


Summing up, I think this would be a minor but not less worth language change to consider for Java 7 (or 8). Especially since everything above should be 100% backward compatible and YOU DON'T HAVE TO USE EM!

What do you think: Take it or leave it?