Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setters for "fluent" api should not be called setX #349

Closed
jecollins opened this issue Sep 20, 2011 · 2 comments
Closed

setters for "fluent" api should not be called setX #349

jecollins opened this issue Sep 20, 2011 · 2 comments

Comments

@jecollins
Copy link
Member

In the Java port, several classes (see Rate for an example) are written in the "fluent" style, where setters can be chained onto the constructor. This improves readability (see RateTests for some examples) and reduces unnecessary verbosity, but it is incompatible with the Java Bean spec, which expects a method called setX to have a void return value.

Two common alternatives are:

  • call them withX(), so you can write new Foo().withBar(a).withBaz(b);
  • call them simply x(), so you can write new Foo().bar(a).baz(b);

Both of these are nice, but if you need to use a setter later, it looks funny to say myFoo.withBar(42); to set a new value for the bar attribute. Is it better to just say myFoo.bar(42);? Perhaps, but not by much.

@jecollins
Copy link
Member Author

In all or at least most cases, these fluent setters are intended to be used to initialize instances that are then supposed to be immutable. Therefore, we will call them withX().

@jecollins
Copy link
Member Author

All fluent setters are now named as in withX(), and some remained setX() but now return void. The distinction is whether the method was intended to be called at object creation time or sometime later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant