2 comments on “Method chaining

  1. The only problem with method chaining in this way is that it is a bit harder to determine the semantics of the method. If you have a method that returns void it is clear that it *must* be a side-effecting method. Something in the universe has changed in a way we can never get back after that method is called. If you return something, though, what does that mean? Has the object instance been modified in some way? Has a new instance been created that is a copy of the original one but with the Name (for example) changed? Your Person example above has the same interface to it that an immutable Person would too, because that is the only way to do it, but it has completely different semantics than an immutable Person object.

    So, while it’s easier on our fingers to do the method chaining version, I would suggest against it unless the semantics of the object matches the interface. Otherwise it becomes very easy to be confused by the API and misuse it, and a good API should make it very hard to misuse.

    One attempt at this is Command Query Response Segregation (CQRS) from Eiffel. It separates doing things from querying them. Microsoft has a series of posts called CQRS Journey. CQRS does have some problems, for example some atomic operations cannot be expressed well in it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s