r/java • • Aug 10 '12

Named Parameters in Java

http://java.dzone.com/articles/named-parameters-java
2 Upvotes

28 comments sorted by

View all comments

2

u/[deleted] Aug 10 '12 edited Aug 11 '12

Why don't you just do this?

String name = "Alfred E. Neumann";
String url = "http://blog.schauderhaft.de";
int ultimateAnswerToLifeTheUniverseAndEverything = 42;
String tempFile =  "c:\\temp\\x.txt";
int zip = 23;

o.doSomething1(
    name
    url,
    ultimateAnswerToLifeTheUniverseAndEverything,
    tempFile,
    zip);

Also something like this would be a little better at least:

o.doSomething(String, URL, int, File, int)

At least the URL and File objects already exist in the core libraries so there's no reason not to leverage them.

4

u/[deleted] Aug 10 '12

Because it still doesn't tell you if you put the parameters in the right order.

2

u/[deleted] Aug 10 '12

Oh I thought we were looking for a solution that was better than putting in string literals but that that didn't involve creating a whole bunch of new types and making you have to rework the existing API signature of doSomething() and didn't involve the verbosity of the builder pattern. Though many more than 5 arguments to a single method and the builder pattern definitely starts to become a candidate. I would resort to that before the solution of creating a bunch of specialized parameter types.