Pure vs Impure Functions
My partner, who is also a developer, once asked me what the difference between functional and object-oriented programming is. There are many differences, and this post explores one of them: the usage of pure and impure functions. What are pure and impure functions? A pure function does not have side effects. A side effect is a change in state that happens outside of a function that gets persisted even after the function goes out of scope. Impure functions are everything else. Here are some examples to clarify. First, a pure function: // Pure int GetSum(int a, int b) { return a + b; } That is a pure function, because it does not affect any state that is outside the scope of the function. Consider this impure equivalent: // Impure int sum = 0; void ComputeSum(int a, int b) { sum = a + b; } That is an impure function, because it modifies the sum variable, and the modification persists even after the method has gone out of scope. When using an object-or...