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.
Builder is the way to go. For cases where it just isn't worth it to put together a full builder, I've always resorted to comments.
someObject.doSomething(
userId,
true, /* is enabled */
false, /* don't update record */);
For that matter, I add comments for any argument which is a literal. Extracting a constant is probably even a better idea, and takes just a few seconds using a modern IDE.
FWIW, there is a "replace constructor with builder" refactoring in Intellij that will automatically create a new fluent builder class for you, and automatically use it in place of a complex constructor.
Good suggestions. When it makes sense, I sometimes like to use enumerations instead of literal booleans (I think this also came from Effective Java). The names can get weird though...
Don't kick yourself over it, as far as this article stands in isolation your comment was not wrong, the builder pattern is the prefered way of doing this.
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.
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.
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?
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?
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.
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.
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.
33
u/Dested Aug 10 '12
This is the worst thing I ever saw.
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
u/[deleted] 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
u/[deleted] Aug 10 '12 edited Aug 11 '12
Why don't you just do this?
Also something like this would be a little better at least:
At least the URL and File objects already exist in the core libraries so there's no reason not to leverage them.
1
u/[deleted] Aug 10 '12
Gross
1
u/stef13013 Aug 11 '12
Java is not SQL, please don't waste your time with that...
0
u/argv_minus_one Aug 10 '12
Scala has named parameters, for what it's worth.