Closed adriano-di-giovanni closed 7 years ago
What kind of computation? And how would you see or observe the outcome of said program?Would it be in a file or on the screen? All the ways you observe program output are side effects.
I think that a REST API server with a pure endpoint is a good example. Isn't it?
No. Network communication is most definitely a side effect. All I/O of any form is a side effect, this is a pretty well-established notion.
That doesn't mean I/O is bad, it just means that since I/O is necessary (and is a side-effect), then programs can never be fully side-effect free.
Is network I/O a side effect even if it's only interface to a pure function?
Side effects are context sensitive. Inside the API, the function is pure, but when you consider the entire system (basically, two distinct entities -- client and server -- that are communicating over the network), it's not. A packet being transmitted over a network interface is an indirect change in the overall state of this system.
I undestand but you say that
the purity of a function is judged from the outside, regardless of what goes on inside.
There's something I can't grasp. A subtle difference that's not evident to me.
Function Purity obeys this law: referential transparency. If the function call can be replaced with its output, and the overall behavior of the system will work exactly the same, it's pure.
In the bigger context of a system including network communication, you couldn't replace a network call with its response without affecting the system, given that network packets have to get exchanged and that changes the state of the internet as a result.
I understand. Thanks.
In chapter 5 you say:
The punchline to this chapter: it's impossible to write a program with no side effects. Well, not impossible; you certainly can. But that program won't do anything useful or observable. If you wrote a program with zero side effects, you wouldn't be able to tell the difference between it and a deleted or empty program.
I think that a good example of a computer program with no side-effects is the one of a program that only does computation.