Environment variables, debugging, globals, etcetera

Suppose that I want to use a global switch to turn certain activity on or off. An example activity would be to save a large amount of state information to disk after an iteration (or not).

There are several ways of achieving this, including passing a flag. Another would be to read the status of the environment variable JULIA_DEBUG or to use my own environment variable, say FOO_BAR.

What are the pros and cons of using FOO_BAR here? Again, I’m thinking of the case in which it is set once in the calling code and not touched again on a run (but whose value is not precompiled).

Alternative suggestions?

If possible, I would try to avoid global configuration variables if possible. e.g. create a “configuration object” that has all of your settings, and pass that around as a parameter to your functions.

Using global state can lead to a long-term headaches — it makes the code harder to compose, e.g. imagine two different packages want to use your code in different ways in the same program.

1 Like

Thanks @stevengj . Yes, that’s what I’ve been doing for most such things. I’d failed to mention that this is something for me to turn on/off while testing, not a user option.

Perhaps @assert alternatives? - #14 by WschW could work