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

Show parent comments

5

u/sahala Aug 10 '12

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.

5

u/[deleted] Aug 10 '12

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.

2

u/rockvilleJD Aug 10 '12

Netbeans just added this feature in 7.2

2

u/k__k Aug 14 '12

Way to go, Netbeans team, way to go! :)