Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Tayo8985y@PublicByte fair enough, but I tried to be smart and turn RPC API interfaces into singletons. At first it seemed like a good idea to reduce CPU usage, but the difference turned out to be negligible and only cause more issues than its worth.
So there's that -
var noob = this.Rant.User.name;
Pro tip:
Explain to the noob what a Singleton is. -
@351483773 pro tip : They never make sense. It's a bad practice (**looks at current code with at least 10 different singletons in project**)
-
When you realize that most (if not all) software objects are a way to manage global state (albeit in a controlled manner), you realize singletons are just one specific way to manage this state. Often singletons are used to manage IO interfaces that can have only one instance. Also, if you want to have multiple singletons later, you can turn the instance call into a mapped generator. This has been particularly useful for testing rigs. Though that didn't have IO involved.
The most fun reason to use a singleton though, is to see noobs cry and moan about why they are evil. -
Tayo8985y@Demolishun singletons do have their uses yeah, I've used them a few times before. One example would be a config class.
In my mind I had a cool application for them in another project, but it just didn't work out like I wanted due to messy state management. That's all. -
pro-tip: there are platforms (for example Unity Engine) where singletons are literally the only way of persisting data/state between scenes (without having to do disk i/o), and i see nothing wrong with that.
-
hjk10157525y@PublicByte globaal state is frowned upon. So being weary about singletons is valid. In the your terminal case you most likely have race conditions unless you guard all the Terminal class variable access with locks. A better solution would likely be if there is some thread safe way to get the shared resources and have the multiple Terminal instances use those. Don't know your code though so I might be really wrong here.
I have also used singletons in PHP stuff that is never going to be concurrent as a way of cashing. It can be solved better, but it works now without modifying the framework. Still would say it's a concept you should not use. -
@hjk101 Heh, if "global state" is frowned upon, then why are our world leaders a bunch of fucktards rushing toward a one world government? ;-)
Pro tip:
Don't make stateful singletons. Just spent an entire fucking morning debugging because one of those fuckers was trying to use prod, and not the demo environment.
rant