For those of you with functional-programming sympathies, what are your thoughts on environmental variables? Do you think they make an otherwise pure function impure?

I'm torn. On the one hand, if a fn is a pure mapping from input => output, it seems pure regardless of how it reads the input. `FOO=1 cmd --bar=2` seems identical `cmd --foo=1 --bar=2`

On the other hand, referential transparency *usually* means being able to reason about a fn from the callsite. And env. vars lose that.


@codesections From the point of view of a function, data is just data. So inside the program it would depend, IMO, on whether the envvar is read just-once along with other inputs and then passed around through pure functions? I would say that pattern would be "pure". But if the envvar might be read multiple times then, maybe it could change between calls? Now it's basically a weird global state, and the functions depending on it may change behaviour even if all call parameters are alike.

Or, put another way: a function that reads input and environment _that is called more than once_ per execution is maybe not ever pure. If the setup/main func is called once only, and no other funcs access outside data, then I reckon you can call it pure.

@codesections Not enjoy.

Mutable global state on a system is eh, especially when it's not integrated with callsites, like you said. Same reason why passing (struct state* state) is better than global variables. (Usually slightly more specific, depending on the program.) This ties into unix programs not working like functions, which causes issues.

@codesections I think "functional programming" needs to be as much about the second part as the first. "Purity" is just ideology run amok.

You have a computer with many levels of state. You can't reasonably pass every field you need down every level.

FP is about minimizing side effects, but you need to get your work done, too. Read the globals and env, even write them as long as you mark the method as a mutator.

@codesections Hmmmm. You could say the same thing about any input who's possible values aren't known before hand, eg networking, user input, etc.

Sign in to participate in the conversation

Fosstodon is an English speaking Mastodon instance that is open to anyone who is interested in technology; particularly free & open source software.