Global state
Of all the preceding points, the most common reason for side effects is the usage of nonlocal variables that share a global state with other parts of the program. Since pure functions, by definition, always return the same output value given the same input arguments, if a function refers to anything outside its internal state, it automatically becomes impure. Furthermore, and this is a hindrance to debugging, to understand what a function has done, you must understand how the state got its current values, and that means understanding all of the past history from your program: not easy!
Let's write a function to detect whether a person is a legal adult by checking whether they were born at least 18 years ago. (Okay, that's not ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access