Command Query separation recommends that every method should either be a Command that performs an action, or a Query that returns data to the caller :
But what about for instance the replace() method of String class.
Its signature is : String replace(String str);
It is violates CQS, isn’t it?
Admitting that it violates, I don’t see how we can fix it so that replace() that is a Command could inform caller of the newest String. Indeed, a command according to CQS must have a void as return type.
I don’t see how it’s a violation – surely it’s a query, in that it asks a question about the data: what would we get if we had the original string, but with X instead of Y each time?
It’s clearly not a command in terms of side effects – it can’t be, as strings are immutable.
I’m not going to claim to be anything approaching an expert on CQS, but it
String.replacesounds a lot more like a query than a command to me.