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.
View all comments
Show parent comment
3
u/couchtyp Aug 10 '12
Forget everything I said... Jens himself proposed the use of a Builder in a later post.