Hacker News new | ask | show | jobs
by billmcneale 498 days ago
Yes, you gain testability.

If your global state contains something that runs in prod but should not run in a testing environment (e.g. a database connection), your global variable based code is now untestable.

Dependency Injection is popular for a very good reason.

1 comments

This sounds like a design deficiency.

If you have something that should only run in testing, perhaps your test harness should set the global variable appropriately, no?

Sure. And a million programmers have all screamed out in horror when they realize that their single test passes, but fails when run as part of the whole suite. Test contamination is a hell paved with global variables.
I can't agree more, I was dying this death a thousand times over at my last job.