• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Is Apple's singleton sample code correct?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is Apple's singleton sample code correct?


  • Subject: Re: Is Apple's singleton sample code correct?
  • From: Uli Kusterer <email@hidden>
  • Date: Mon, 28 Nov 2005 09:54:18 +0100

Listening to this thread going on and on, I can't help but think there's over-engineering going on. The OP was simply wondering whether there was a reason for Apple's singleton sample code being so complicated, and it looks like we've found it's not really in any way necessary to have this code for the typical singleton (i.e. like +sharedWorkspace).

The case where this singleton code seems not un-sensible is for constant stuff, such as [NSNumber numberWithInt:1], though even there I'd personally consider it a matter of taste whether you'd want to go to such lengths.

I can see how one would warn or prohibit certain memory management practices on a singleton to catch usage bugs (like deallocation through unbalanced -release calls, which is easily detected for an object that's supposed to live as long as the app). This is especially important when you have lots of projects, or co-workers who may not be familiar with the code you wrote.

Apart from those considerations, I really think the reduction of lines of code (as Wil Shipley would put it: "a deleted line of code is a debugged line of code") is much more important than trying to come up with a complex safety net of code that only introduces new bugs. Just look at the code samples posted and all the bugs people pointed out in them.

So, different from other Apple recommendations, I'd suggest to every newbie to just ignore this one. Write the simplest singleton code: A class-level accessor that lazily instantiates a regular object into a static variable. Maybe override -dealloc to NSLog() a warning. Leave the more complicated stuff until you're actually tracking down an insiduous bug that seems to be related to the singleton or once you've profiled using Shark and know for sure that a particular singleton is a bottleneck and needs to be optimised.

Just my $0.02.

Cheers,
-- M. Uli Kusterer
http://www.zathras.de


_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: Is Apple's singleton sample code correct?
      • From: jkp <email@hidden>
References: 
 >Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Shawn Erickson <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Uli Kusterer <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Uli Kusterer <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: mmalcolm crawford <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: mmalcolm crawford <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Christian Brunschen <email@hidden>)

  • Prev by Date: Re: Is Apple's singleton sample code correct?
  • Next by Date: Re: Tree Data Structure in objc-C?
  • Previous by thread: Re: Is Apple's singleton sample code correct?
  • Next by thread: Re: Is Apple's singleton sample code correct?
  • Index(es):
    • Date
    • Thread