Comparison
Command-query separationvsTell, don't ask
Command-query separation
you called something that looked like a getter and it wrote a row to the database.
A method should either change something or answer something, never both. When a query has a side effect, nobody can safely call it twice, log it, cache it or move it inside a conditional — all the things people do to a getter without thinking. The cost of ignoring it is a class of bug that only appears when someone removes a call they believed was a read.
Full entry →Tell, don't ask
the caller pulled three fields out, decided something, and wrote one back — a decision the object should have made itself.
The heuristic that you should send an object a message describing what you want done, rather than interrogating it and acting on its data from outside. It is the practical form of encapsulation: behaviour lives with the data it needs. The counterweight is that not everything is an object with behaviour — a value being reported to a user or serialised has to be asked for, and forcing it through a command is worse than the smell it avoids.
Full entry →