6

The company I'm working for has a HUGE class (Globals.m) that is a singleton. It stores mostly a bunch of BOOLs that should go into NSUserDefaults, but there are also pointers to things like videoplayers that belong to various viewcontrollers. I am fairly new to iOS development (and I'm a lowly intern), but I know in my gut that the code smells to high heaven. How can I explain why using a singleton to store all the variables is bad?

Edit: I don't mean singletons are bad, I just mean in this case. Essentially every significant variable in the app is stored in this one instance.

Febble
  • 205
  • 2
  • 7
  • 1
    A pattern is just a pattern... What is really bad is to blindly tell something is bad just because you think it is... Singletons are not bad, but you can of course use/create bad singletons... It depends how you design/use them... – Macmade Aug 23 '13 at 19:39
  • Singleton it's a pattern... don't use appropriately it's bad and I smell that somebody think that are using singleton when really it's not singleton – Zach dev Aug 23 '13 at 19:42
  • Thanks @JoshCaswell, I saw that earlier, but I didn't quite follow all of his points. Sorry for the repetition! – Febble Aug 23 '13 at 19:58

2 Answers2

4

A lot of objects in UIKit are singletons. The UIApplication object is a singleton. NSUserDefaults has the standardUserDefaults singleton. UIDevice currentDevice returns a singleton.

Just because it's a singleton doesn't mean it's bad. What is bad is when you start tying functionality in other classes to your singleton object, with it so deeply ingrained that you can't alter the singleton or the affected object easily.

I use a singleton to store my non-CoreData object structures. I also define some helper methods for getting the library directory, encoding data, and keyed archiving. So I can reference a master array object wherever I need it, or easily access methods that would otherwise just be copy and pastes.

Justin
  • 9,042
  • 7
  • 67
  • 97
  • So if all they are using it for is variables, then there is no harm? – Febble Aug 23 '13 at 19:54
  • I'd say that if you're directly accessing and modifying objects of your singleton within View Controller code, instead of assigning those objects as properties of the View Controller class, that's when it's bad. ie `-viewDidLoad { [[[Singleton shared] array]addObject:newObject]; }` would be bad, versus setting a property for the array in your View Controller and modifying it there. – Justin Aug 23 '13 at 20:36
0

Globals.m doesn't sound really like a singleton, and if it is, it looks like it's just used for configuration purposes? Then a file full of #defines or extern would do the same, I guess. Without any more details it's hard to know what you're searching.

Vik
  • 1,877
  • 12
  • 18
  • It is set up as a singleton and explicitly has a comment that refers to it as a singleton. And it's being actively used for storing any variable that has to access other classes, not just for configuration. Aside from UI, don't actually reference each other. Everything is done via NSNotificationCenter and this Globals instance. It is very possible that I just jumped to the conclusion that it's bad practise, like I said, I'm not very experienced. – Febble Aug 23 '13 at 19:48