DEV Community

Discussion on: Should Pure Functions Be Self-contained

Collapse
 
t4rzsan profile image
Jakob Christensen

As you know a pure function has the following two requirements:

  1. It must not have any side effects, i.e. it is not allowed to change any state of the program or of external ressources.
  2. It must be deterministic, i.e. for a given set of inputs it must almost return the same output. This is also called memoizable.

That does not imply that you cannot call other functions but if the called functions change behaviour you are breaking rule #2.

Functional programming languages have immutable values (at least by default) so you cannot "override" functions or change variables. By definition an immutable variable is not really a variable but just a value.

If you are trying to do functional programming in JavaScript or C# which have references, well, then you have to enforce those rules by coding style and self-discipline. The runtime will not help you.