How Bad Are Singletons? How Bad Are Singletons? codeigniter codeigniter

How Bad Are Singletons?


Because it's relatively easy to work with singletons, and working without takes much more detailed planning of your application's structure. I asked a question about alternatives some time ago, and got interesting answers.


People discourage the use of global variables because it increases the likeliness of bugs. It's so much easier to make a mistake somewhere if every function in your program accesses the same global variables, and it's much harder to debug. It's also much harder to test for.

I imagine they're still used so often because programmers are lazy. We don't want to spend time up-front making the code all organized and pretty, we just want to get the job done. It's so much easier to just write a global function/variable/whatever than to modularize it, and once you've started down that path it's too much of a pain to go back and refactor. Maybe that's the reason: They started out that way and simply never turned back.


Probably because of the Design Patterns book by the GoF. It's become too pervasive and people treat it as infallible.