Re:Singleton Is a Bad Idea
by emodendroket on Sunday April 10, 2022 @02:49PM
(#30980382)
It's like when you teach students not to begin sentences with "and." Sure, in practice, very sparingly, that can make sense. But the problem is that beginners think that almost everything is a case for them.
It's fine to have an object that is only instantiated once. But there's no reason to enforce that in the class.
Re:Singleton Is a Bad Idea
by Etherlord87 on Sunday April 10, 2022 @03:47PM
(#30980989)
So, as per the argument in the submission, you should create a function that manages that. And then you want to make sure the class is not instantiated other than with that function. So you recreate a singleton, but one of the methods is outside the class, is harder to find, easier to forget about when moving code around etc.
Yes, making sure no one makes more than one instance of the class is defensive programming and you could consider it bad, but a lot of techniques in programming are defensive like that, especially in OOP.
Singletons are a tool, that at some point you just can't avoid. We can discuss the way to create or access them, but saying they are just all bad is a diservice to younger engineers that are learning proper paterns.