News for hackers. Stuff that matters.
Re:So Singletons are bad, then what? by csmithuk on Thursday January 30, 2014 @10:02AM (#7150975)
Singletons are fine. It's just some idiots plug everything into them then realise they've blow their toes off with a monolithic ball of mud architecture.

I use a singleton on every project which is OO based but it only ever holds the service locator/container.

Asp.net MVC is a fine real world example of how to fuck up Singletons (routing dictionary, global action filters etc). Totally stinks and is coupled like glue.

Re:So Singletons are bad, then what? by mattgreenrocks on Thursday January 30, 2014 @10:31AM (#7151126)
> I use a singleton on every project which is OO based but it only ever holds the service locator/container.

iOS devs love singletons; it seems like almost everything is a sharedSomething. Definitely a design smell.

Infrastructure can sometimes call for a singleton. It's very convenient to have something that is only created when it is needed. That way, users only pay for what they use, and they don't have to bear the conceptual burden of toting an extra context object around.

But it is a sledgehammer, and the people most inclined to abuse it lack the ability to tell the damage it is doing to the design.

Re:So Singletons are bad, then what? by frou_dh on Thursday January 30, 2014 @11:02AM (#7151335)
It's mildly interesting to note that in practice essentially every GUI application on OS X is a singleton because the OS makes a strong distinction between application instances and windows (compared to e.g. MS Windows where the distinction is muddy).

Launch TextEdit.app twice and there is still only one running instance of the application. If you want a second window, you open a second window.

    # instance trivia:
    $ open -n /path/to.app
Re:So Singletons are bad, then what? by dllthomas on Thursday January 30, 2014 @11:26AM (#7151485)
This is why it's nice to be able to parameterize "these are the actions I want done" with "here's how you combine them".
Re:So Singletons are bad, then what? by csmithuk on Thursday January 30, 2014 @02:16PM (#7152731)
Tell me about it. To get single instance behaviour on windows you have to piss around with global mutexes.
Re:So Singletons are bad, then what? by masklinn on Thursday January 30, 2014 @02:01PM (#7152629)
That's not being a singleton since you can have a second instance if you ask for it.
Re:So Singletons are bad, then what? by frou_dh on Thursday January 30, 2014 @04:04PM (#7153348)
Odd that the pair of you are nitpicking since I both said "in practice" and gave a command that could be used to produce that screenshot.
Re:So Singletons are bad, then what? by masklinn on Friday January 31, 2014 @03:45AM (#7155910)
> Odd that the pair of you are nitpicking

It's not nitpicking, half of the singleton's purpose is to only ever have a single instance.

Re:So Singletons are bad, then what? by frou_dh on Friday January 31, 2014 @05:40AM (#7156120)
You're still not telling me anything I don't know.
Re:So Singletons are bad, then what? by masklinn on Friday January 31, 2014 @12:16PM (#7157742)
If you knew it was wrong in the first place, why post it?
Re:So Singletons are bad, then what? by frou_dh on Friday January 31, 2014 @02:10PM (#7158524)
I now realise I was providing reading comprehension training†

† NOT, I REPEAT NOT, a literal course of training. I want to make it doubly clear that I did not certify anyone and can not be held responsible for any of their future actions vis-a-vis reading or comprehending.

Re:So Singletons are bad, then what? by kd0amg on Thursday January 30, 2014 @02:42PM (#7152877)
Re:So Singletons are bad, then what? by darbelo on Thursday January 30, 2014 @11:35AM (#7151539)
My favorite sight when I review iOS code for clients is seeing a method on the App delegate that 'preloads' a bunch of singletons in one go. It sets the tone for the rest of the trip.

In my experience, a large bunch of iOS developers tend to use singletons out of imitation. The "sharedWhatever" is the usual way for the framework to provide access to anything that is purposefuly single-instance[1]. Same thing with delegation, though that one is usually less cringe-worthy in practice.

[1] The "the accelerometer on your phone" kind of single-instance, which is a much more defensible use-case than what the typical iOS coder uses them for.

Re:So Singletons are bad, then what? by aliston on Thursday January 30, 2014 @01:54PM (#7152581)
So, have we decided on what would be a better approach for storing global state?

Static variables and methods? Pass the instance down to all children?

Re:So Singletons are bad, then what? by mattgreenrocks on Friday January 31, 2014 @07:16AM (#7156299)
It seems like there's no hard and fast rule.

I dislike singletons, but will gladly make a static method + static var from time to time.

Re:So Singletons are bad, then what? by nephorider on Thursday January 30, 2014 @11:30AM (#7151516)
IMHO, and to avoid abuse, singletons in iOS/MacOS should not be considered as a pattern that can replace class methods or simple C utility function. And this in all ways. I have seen piece of code doing everything with class methods while a set of plain C functions would have been more adequate (those were really utilities)