4 ms·
It shares the crucial feature which makes global variables considered to be so bad. Namely: "Where on earth is this set to X, it should be Y at this point!"
by sharpneli 13y ago
It shares the crucial feature which makes global variables considered to be so bad.
Namely: "Where on earth is this set to X, it should be Y at this point!"
- kabdib 13y agoWhereupon you can put tracing in to find out when the state was screwed up. Singletons aren't bad, they're just misused a lot by bad programmers. It's like saying "Food is bad," and sure, people misuse food and hurt themselves, but that doesn't mean we should ban food.
- dllthomas 13y agoExcept it's nothing like saying "food is bad". If you don't ever eat food, you die. If you don't ever use singletons, some of your code has an additional parameter uglily threaded through it.
- gohrt 13y agoIt's more like saying "poison is bad", and losing the benefits of bleaching the bathtub, just because people keep killing their kids with it by accident.
- iopq 13y ago> Whereupon you can put tracing in to find out when the state was screwed up. And then the problem magically disappears, but only when you trace it.
- dllthomas 13y agohttp://en.wikipedia.org/wiki/Heisenbug http://en.wikipedia.org/wiki/Heisenbug
- kabdib 13y agoTracing can be very lightweight. Not utterly non-instrusive, but pretty close. As a kernel / games dev, I'm quite aware of timing issues. You get into serious trouble with "lock free" stuff, where pretty much any additional activity (even register operations) will perturb things and make bugs go away. Hopefully at this point you have help from the hardware. Then again, if you're doing LF, you're in a special place and probably have the chops to deal with it...
- dllthomas 13y agoThat is a feature of unconstrained, shared, mutable state. While singletons are often used that way, there are two problems with the resulting code. On the other hand, imagine a hypothetical situation where you have a bunch of static data that you would prefer is only generated once, and only ever generated if needed. A singleton gets this done, without any sort of "where on earth is this set to X?" but retains issues with injection for testing and the like (depending on precisely how it's implemented, in most languages).