2

Are there well established terms for referring to methods that do or do not mutate the object state?

For example of a well established term describing a type of function: "idempotent".

hasen
  • 161,647
  • 65
  • 194
  • 231
  • 1
    Eric Evans makes reference to 'side-effect free functions' in the DDD book. Googling the term reveals that it has wider significance than just DDD. There's also Command/Query Separation (http://martinfowler.com/bliki/CommandQuerySeparation.html) to consider too. – David Osborne Apr 07 '15 at 16:14

1 Answers1

1

Pure Function is a well-established term referring to an operation which depends solely on its input arguments. It neither accesses nor mutates any non-local state. The concept of a pure function in programming is derived from mathematical functions. The opposite of a pure function is an impure function, which may perform IO, produce a side effect, or have some dependency on non-local state.

Note that the definition of a pure function is more strict than what this question asks for, since a pure function prohibits not only mutation of object state but also access to object state. To find a definition which fits between pure and impure functions, we can consider the Difference between a method and a function. Often, these two terms are conflated into one amorphous abstraction; but for the purpose of this question we will use more precise meanings: a method is coupled to an object whereas a function is independent.

From that simple definition we naturally reach the concept of Pure Methods. A pure method is just a pure function with one difference: in addition to its input arguments, the method implicitly receives a reference to its enclosing object, as if it had one extra argument.

I can't say that the term pure method is well established in the same way as pure function; but if we take the definition of a pure function, alongside the definition of a method, then I think the definition of a pure method follows intuitively. So to answer the question, pure is the keyword commonly used to indicate the presence or absence of mutation.

Community
  • 1
  • 1
jaco0646
  • 15,303
  • 7
  • 59
  • 83