📎 Webclip
Builders and Manipulators
The article argues for a strict split in OOP method names. If a method returns a value, it should be a noun that names what it returns. If it changes state, it should be a verb and return void. The point is to keep queries and commands separate and make the code easier to understand.
Reading notes#
- Builders create or find an object and return it.
- A builder name should describe the returned result, so
book()is preferred overfind(),getTitle(), orcalculateSpeed(). - Builders should not modify the encapsulated entities and should have no side effects.
- Manipulators change the entities the object encapsulates.
- Manipulators should return
void, as inadd(Book book)orsave(). - The rule aims to separate declarative code from imperative code.
- Following the rule is said to make methods smaller, names shorter, and design simpler.
- The author links the idea to Bertrand Meyer’s distinction between queries and commands.
- The author rejects the noun/verb naming principle because it does not separate builders from manipulators.
