News for hackers. Stuff that matters.
Re:Singleton Is a Bad Idea by javajosh on Sunday April 10, 2022 @02:35PM (#30980206)
Hard disagree. Singleton is a tool, and like any tool it has trade-offs. Singletons help you target a very specific, useful time in your application: after it's compiled, but before it's serving requests. You can call this the "initialization time". Within this time, singletons help you organize the one-off components of your application - canonincally, things like connection/thread pools.

But hey, thanks for the hot take.

Re:Singleton Is a Bad Idea by cr__ on Sunday April 10, 2022 @02:50PM (#30980397)
Why not explicitly initialize the one-off components and pass them to the other parts of the system that need them as arguments so the system is testable and dependencies are clear?

(I know why, generally—low-expressivity languages make it nightmarish to actually do this.)

Re:Singleton Is a Bad Idea by nedbat on Sunday April 10, 2022 @03:03PM (#30980525)
If you need just one, make just one. Why should the class enforce it?
Re:Singleton Is a Bad Idea by thfuran on Sunday April 10, 2022 @04:23PM (#30981333)
Why deliberately avoid enforcing design constraints? I mean, why not just make everything a void * and cast as needed and promise not to mess up?
Re:Singleton Is a Bad Idea by nedbat on Sunday April 10, 2022 @05:53PM (#30982089)
You can enforce the constraint, but it shouldn't be in the class itself. How to you make instances for tests?