I started writing a fluent interface and took a look at an older piece Martin Fowler wrote on fluent interfaces (which I didn’t realize he and Eric Evans coined the term). In the piece, Martin mentions that setters usually return an instance of the object being configured or worked on, which he says is a violation of CQS.
The common convention in the curly brace world is that modifier
methods are void, which I like because it follows the principle of
CommandQuerySeparation. This convention does get in the way of a
fluent interface, so I’m inclined to suspend the convention for this
case.
So if my fluent interface does something like:
myObject
.useRepository("Stuff")
.withTransactionSupport()
.retries(3)
.logWarnings()
.logErrors();
Is this truly a violation of CQS?
UPDATE I broke out my sample to show logging warnings and errors as separate behaviors.
No. The pattern here is “Configuration”. Such configuration commands return the configuration object itself as opposite to something unrelated to the command. A violation of the Command/Query segregation would occur if the commands which serve the configuration purpose returned some unrelated data, for example: