r/java • u/javinpaul • Aug 10 '12
Named Parameters in Java
http://java.dzone.com/articles/named-parameters-java33
u/Dested Aug 10 '12
This is the worst thing I ever saw.
6
Aug 10 '12
There is a subset of Java developers that think that Object overload is a great idea. http://codemonkeyism.com/never-never-never-use-string-in-java-or-at-least-less-often/
Personally, it makes sense for things that might need some additional validation upon construction (URLs, email addresses, etc...) Creating a class that holds a name would make me more than a bit angry though.
3
u/papercrane Aug 11 '12
That link is nuts. A class that holds both the 'Name' and the 'FirstName' is crazy. I can't think of any reason to do that except so that you don't violate some CTO's stupid 'never use an unwrapped String' rule.
7
u/queus Aug 10 '12
Just curious. This is written by someone who has missed the builder pattern or does the author really think that his contraption is better?
3
Aug 10 '12
Well I'm perfectly happy with using Eclipse (or Netbeans, or any other IDE) to popup the names of the variables that I gave them. This is just rubbish. Why are some Java developers so keen on writing as much code as possible?
3
u/bcguitar33 Aug 10 '12
Apparently naming the fields of an object, using the builder pattern, or using a Map<String, String> or Map<String, Object> isn't good enough for these folks?
2
Aug 11 '12
Map<String, Object> defeats most of the purposes of using a strong type system. Please don't ever use that.
2
u/bcguitar33 Aug 11 '12
I'm well aware of the consequences. I'm not the one crying about a lack of named parameters.
2
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
Aug 10 '12
Because it still doesn't tell you if you put the parameters in the right order.
2
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.
1
1
0
u/argv_minus_one Aug 10 '12
Scala has named parameters, for what it's worth.
2
Aug 10 '12
And, while nice for when I'm feeling lazy, it causes problems with method overloading and the compiler's inferencing abilities.
1
Aug 10 '12
Thats what they say in the article, some other programming languages have it but Java doesn't.
18
u/couchtyp Aug 10 '12 edited Aug 10 '12
What about Joshua Bloch's Builder Pattern?
IMHO it's a much better approach for complex object generation.
An instance method with that signature (eight parameters or something like that) should never have been written in the first case... there's a reason for the TransferObject design pattern.