Skip to content

Style Guide

Nick Irvine edited this page Nov 21, 2018 · 1 revision

Avoid environment variables in library code

Environment variables break encapsulation, badly. They're essential global variables that are never declared, only referenced.

And that doesn't just affect maintainability, readability, etc. Library consumers can't safely use your library in multithreaded code safely without locks around the environment.

Consider that consumers of the library include its own test suite: the tests have to do a bunch of env var setup/teardown to ensure they're not in unexpected (global) state. That's hard to get right.

What to do?

Only code in a main package should look at environment variables.

Clone this wiki locally