jargon

Comparison

Global mutable statevsSingleton

Global mutable state

the test passed alone and failed in the suite, and the difference was something another test had left behind.

State reachable from anywhere and writable by anyone, whether it is spelled as a global variable, a static field or a singleton. It makes execution order significant, makes tests interdependent, makes concurrency unsafe by default, and makes the dependency invisible in every signature. This is what people mean when they call the singleton an anti-pattern: the objection is almost never the instance count.

Full entry →

Singleton

two tests passed on their own and failed together, because the thing they both reached for kept state between them.

A class that permits exactly one instance and provides global access to it. The instance-count half is almost never the requirement — what people actually want is 'one connection pool', which lifetime configuration in a container gives them without the global. The access half is the damage: an unreplaceable dependency nothing declares, invisible in every signature, and shared across tests that were supposed to be independent.

Full entry →

Related comparisons